2

I'm reading a book C progaming faq's. Here is passage of the book

Automatic variables are variables defined inside a function or block of code without the static keyword. These variables have undefined values if you don’t explicitly initialize them. If you don’t initialize an automatic variable, you must make sure you assign to it before using the value.

Here is my code:

#include <stdio.h>
int main (int argc, const char * argv[])
{    
    {
        int x;
        printf("%d", x);
    }
}

The result of printf is 0. Why is the variable initialized?

undur_gongor
  • 15,657
  • 5
  • 63
  • 75
pr1m3x
  • 1,947
  • 6
  • 21
  • 19
  • Which environment (OS, compiler etc)? – fge Jan 05 '12 at 13:40
  • 1
    Who say it is initialized ? it might just be luck that the data in memory is actually zero ? I think debug mode will automatically initialize a lot variables to zero; have you tried building and running in release ? – Max Jan 05 '12 at 13:41
  • 3
    Did you expect the output to be "uninitialized"? :) – pmg Jan 05 '12 at 14:03

7 Answers7

7

For static and global variables it is 0; automatic variables are not initialized by default.

in the c language there is no default value for non static local variables. The variable holds whatever was in memory before it became a variable. It's best to always initialize a non static local variable before using it in the c language (or at least before comparing it to something else). Also It's best to assume that there is no default value because this varies from language to language, and hardware to hardware.

Read more: http://wiki.answers.com/Q/What_is_the_default_value_of_integer_in_c#ixzz1iaij7hRK

MRM
  • 435
  • 2
  • 10
4

It isn't initialized. The memory cell your x occupies still has a value from earlier use. However, the value of x might be anything, so you can't rely on it.

thiton
  • 35,651
  • 4
  • 70
  • 100
1

Undefined means it could be anything, even 0. Another implementation may have a different or random value. There's no way to know, and you can't trust it to be the same on every execution either.

harald
  • 5,976
  • 1
  • 24
  • 41
1

Some compilers will do default initializations for you, some won't. You shouldn't count on them. The line saying

int x;

should say

int x = 0;

if that's what you want. For some C compilers, it's possible that x is -23157263 at the point printf is called.

Ben Hocking
  • 7,790
  • 5
  • 37
  • 52
1

The initial value of an automatic variable if not explicitly initialized is indeterminate.

Indeterminate means either unspecified (could be 0, or 42) or a trap representation. A trap means reading the uninitialized variable undefined behavior (for example, crash you program).

ouah
  • 142,963
  • 15
  • 272
  • 331
0

Because at the memory space where x is placed there is already a 0, but this is just "luck" there could be any number or symbol at that place. So to be sure that always get the same result you should always initialize a variable.

TobiasW
  • 378
  • 1
  • 5
  • 17
0

I recall that Visual Studio would (and perhaps still does) set auto variables to 0 or null in some cases when the code was compiled with debug flags. Like others have said, you certainly should not count on any default values.

Todd Murray
  • 423
  • 2
  • 7