#include<stdio.h>
int main()
{
int x;
printf("%d\n", x);
x=x>0?x:x+1;
printf("%d\n", x);
}
In this x is initialized to 0, but why? Wrote and compiled the code in GCC
#include<stdio.h>
int main()
{
int x;
printf("%d\n", x);
x=x>0?x:x+1;
printf("%d\n", x);
}
In this x is initialized to 0, but why? Wrote and compiled the code in GCC
Use of uninitialized variable is cause for undefined behavior. One compiler may initialize the variable to 0
, but that could easily change if you change compilers or even by changes to compile flags, such as using different optimization levels.
Don't count on it.
It is an undefined behavior. You are not initializing the value for x. If you are not doing that , then it take some garbage value. Garbage value can any value we can't expect that this value will come. So simply initialize the variable you are using.