Saturday, November 7, 2015

Funny C Program:Comparison Between Zhishen Lu Eating Steamed Bun v5 and v6

Acturally,I mentioned the difference between the 5th version and 6th one in Funny C Program:Zhishen Lu Eating Steamed Bun v6,so I am not going to repeat it.Let's discuss whether to assign values to every array element one by one through a loop statement or not.To find the reasonable explanation,I tested a few programs on C4droid.

//Testing program 1
#include <stdio.h>
int main(void)
{
int i,array[101]={0};
for(i=1;i<101;i++)
printf(" %d",array[i]);
return 0;
}

Following is the result.


Testing program 1


What?It depends on the scale of the array?
I was puzzled,and modified the code like testing program 2.

//Testing program 2
#include <stdio.h>
int main(void)
{
int i,array[3]={0};
for(i=0;i<3;i++)
printf(" %d",array[i]);
printf("\n");
return 0;
}

The result is following.


Testing program 2

It seemed that scale of the array did influence the result.To convince myself,testing program 3 came into being.Of course there were many other testing programs which I'm unable to show to you one after another.

//Testing program 3
#include <stdio.h>
int main(void)
{
int i,array[6]={0};
for(i=0;i<6;i++)
printf(" %d",array[i]);
printf("\n");
return 0;

}

And the result is following.


Testing program 3


As expected,not all the elements equal 0.
How can we explain it?Maybe it's because different compilers deal with "int a[10]={0};" variously,I think.To avoid mistakes,why not write a loop statement?I'm sorry that I didn't want to write another circle statement,so the mistake appeared.But I feel lucky for learning so much from it.
Of course,if you understand the exact explanation of this mistake,welcome to post a comment or email me at jimzhou001@gmail.com.Thank you!

No comments:

Post a Comment