-2

I have some doubt in this simple program.

  void hello()
  {
      int a;
      printf("uninitialized in hello: %d\n",a);
  }

  int main()
  {
      int value;
      printf("uninitialized in main: %d\n",value);
      hello();
      return 0;
  }

If i am compile and run this program.

  user@toad:~$ gcc -Wall simple.c
  simple.c: In function ‘hello’:
  simple.c:6:3: warning: ‘a’ is used uninitialized in this function [-Wuninitialized]
       printf("uninitialized in hello: %d\n",a);
       ^
  simple.c: In function ‘main’:
  simple.c:12:11: warning: ‘value’ is used uninitialized in this function [-Wuninitialized]
       printf("uninitialized in main: %d\n",value);
       ^

  user@toad:~$ ./a.out 
  uninitialized in main: 0
  uninitialized in hello: 32672
  user@toad:~$ ./a.out 
  uninitialized in main: 0
  uninitialized in hello: 32679
  user@toad:~$ ./a.out 
  uninitialized in main: 0
  uninitialized in hello: 32599
  user@toad:~$ ./a.out 
  uninitialized in main: 0
  uninitialized in hello: 32560
  user@toad:~$ ./a.out 
  uninitialized in main: 0
  uninitialized in hello: 32585

Why main() function prints

uninitialized in main: 0

but hello() function prints

uninitialized in hello: [some garbage value.]

Is it is undefined behaviour?

sakthi
  • 675
  • 2
  • 10
  • 18
  • 5
    Zero is a possible garbage value. – M Oehm Mar 09 '16 at 10:00
  • 2
    You should not seek explanation for `undefined behavior` .The answer would be platform as well as implementation dependent. – Vagish Mar 09 '16 at 10:00
  • but every time i got zero.is it is garbage. – sakthi Mar 09 '16 at 10:02
  • 2
    Yes, any value read from an uninitialised variable must be considered as garbage. And just because your simple program shows it as zero for the few times you ran it does not mean it will actually always be zero. Try adding in some more unrelated code and chances are at some point it will show some other garbage value. – kaylum Mar 09 '16 at 10:05
  • When you have your compiler set to generate a debug build, some bad compilers like VS stupidly sets all of the stack to zero. (Which is very bad, because it hides all bugs related to initialization and make them pop up far later, in release build only.) Meaning everything there is zero until used. When you call functions, various things are stored on the stack, likely non-zero values. This is not specified behavior though, and nothing you can rely upon. – Lundin Mar 09 '16 at 10:15

1 Answers1

2

For the unitialized automatic local variable, the value is indeterminate. That can be anything, including 0.

Quoting C11, chapter §6.7.9, Initialization

If an object that has automatic storage duration is not initialized explicitly, its value is indeterminate. [...]

FWIW, any attempt to make use of that value, (including attempt to print), will invoke undefined behavior.

Related, from the annex J, for undefined behavior

The value of an object with automatic storage duration is used while it is indeterminate.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • To be picky, it will only invoke undefined behavior if the value is a trap representation. Otherwise the value is just indeterminate - some random garbage. On a system without trap representations, it is perfectly fine to do things like `int x; printf("%d", x);`, though of course you can't know what it will print - it is _unspecified behavior_. Annex J of the C standard is not normative and refers to trap representations. – Lundin Mar 09 '16 at 10:18