Problem: Local variable is getting initialized to zero, but if assign the variable to a pointer it is not getting initialized to zero.
Code 1: Local variable is getting initialized to zero (I have tested it may times, a is always getting set to zero)
int main() {
int a;
printf ("a = %d\n",a);
return 0;
}
OUTPUT:
a = 0 ( I have called this about a 100 times, and 'a' is always zero)
Code 2: Local variable is NOT getting initialized to zero (I have tested it may times, a is never getting set to zero)
int main() {
int a;
printf ("a = %d\n",a);
nt *b = &a;
printf("*b = %d\n",*b);
return 0;
}
OUTPUT:
a = 21683 (everytime I run, I get different values as expected)
*b = 21683
I expected a to have random values as it is not a static variable. Can anyone has any idea as to why this happening? Is this something in the standard or some feature of the compiler.
PS: I am using gcc.
I have tried different optimization options, even -o0 but the result is the same.