-1
#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

Divya
  • 393
  • 2
  • 5
  • 17
  • value of uninitialized variable depends on compiler. It can be either 0 of a junk value based on heap handling of the compiler – Ashutosh Nigam Mar 23 '15 at 05:07

2 Answers2

1

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.

R Sahu
  • 204,454
  • 14
  • 159
  • 270
1

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.

Karthikeyan.R.S
  • 3,991
  • 1
  • 19
  • 31