Friday, 9 August 2013

Automatic variable in C not initialized but given fixed value within loop

Automatic variable in C not initialized but given fixed value within loop

I read that automatic variables in C, if not initialized to values,
contain garbage values. However, I found something that did not correspond
to my understanding. Consider the following piece of code.
#include <stdio.h>
main()
{
int i = 0;
for (i = 0 ; i< 10; i++)
{
int x;
int a = 500;
printf("%d\t%d\n", a, x);
}
}
Output:
500 2
500 2
500 2
500 2
500 2
500 2
500 2
500 2
500 2
500 2
My question is - shouldn't I be expecting garbage values instead of 2s? I
ran the program on Dev-C++ on a Windows machine.

No comments:

Post a Comment