61

Possible Duplicate:
Why are global and static variables initialized to their default values?

See the code,

#include <stdio.h>

int a;
int main(void)
{
    int i;
    printf("%d %d\n", a, i);
}

Output

0 8683508

Here 'a' is initialized with '0', but 'i' is initialized with a 'junk value'. Why?

1
  • 1
    Closed, but the most obvious reason is not mentioned: local variables are just pointers to the stack, and the stack changes all the time.
    – dagelf
    Commented Nov 6, 2020 at 11:38

4 Answers 4

132

Because that's the way it is, according to the C Standard. The reason for that is efficiency:

  • static variables are initialized at compile-time, since their address is known and fixed. Initializing them to 0 does not incur a runtime cost.

  • automatic variables can have different addresses for different calls and would have to be initialized at runtime each time the function is called, incurring a runtime cost that may not be needed. If you do need that initialization, then request it.

10
  • 1
    nice answer +1,...global values are assigned 0 at compilation time na? Commented Dec 27, 2012 at 5:45
  • 7
    If the global variables are not initialized to a non-zero value, they are set to zero as the process image is loaded, before the main program starts. That's a one-time operation, compared with an 'every time the function is called' operation for local variables. Commented Dec 27, 2012 at 7:03
  • 20
    actually static variables are initialized at runtime too. The C runtime (crt) will initialize them before calling main. Of course this happens only once but it still at runtime.
    – Sil
    Commented Oct 9, 2013 at 13:46
  • 13
    Also, you cannot initialize anything at compile time. The program needs to be started and loaded in memory so that you can 0 out the contents (in this case the bss section). It is impossible to that at compile time, only runtime. cheers
    – Sil
    Commented Oct 9, 2013 at 13:48
  • @Sil - from the instances that I examined you are right in your 1st comment. However, can you justify the 2nd one? Since the variables allocation is known at load time, why do you think it is not possible to have the initialization values put in the binary image, and set the variables during image load?
    – ysap
    Commented Nov 18, 2014 at 19:08
44

global and static variables are stored in the Data Segment (DS) when initialized and block start by symbol (BSS)` when uninitialized.

These variables have a fixed memory location, and memory is allocated at compile time.

Thus global and static variables have '0' as their default values.

Whereas auto variables are stored on the stack, and they do not have a fixed memory location.

Memory is allocated to auto variables at runtime, but not at compile time. Hence auto variables have their default value as garbage.

1
  • 6
    That's not completely correct. .bss is not allocated during compile time, and actually this is the reason for introducting .bss as a special section for uninitialized/zero static/global variables. Only size information need to be stored and hence the binary isn't trashed with unnecessary data (i.e. zeros). At runtime, .bss variables are initialized with zero (unlike variables in the .data section, where the actual initial value has to be stored within the binary). Besides that, I consider this as the better answer, +1 :-)
    – andreee
    Commented Dec 18, 2015 at 21:03
17

You've chosen simple variables, but consider:

void matrix_manipulation(void)
{
    int matrix1[100][100];
    int matrix2[100][100];
    int matrix3[100][100];

    /* code to read values for matrix1 from a file */
    /* code to read values for matrix2 from a file */
    /* code to multiply matrix1 by matrix2 storing the result in matrix3 */
    /* code to use matrix3 somehow */
}

If the system initialized the arrays to 0, the effort would be wasted; the initialization is overwritten by the rest of the function. C avoids hidden costs whenever possible.

3
  • 1
    The effort would be wasted were they simple variables as well...
    – K-ballo
    Commented Dec 27, 2012 at 5:41
  • 2
    @K-ballo: yes, you're right — but the cost is more noticeable when the variables are largish arrays, or structures, or arrays of structures. Even a few dozen integer variables being initialized is not very noticeable, but a few thousand integer values becomes noticeable. Commented Dec 27, 2012 at 5:45
  • 1
    even 1 integer is noticeable (+1 instruction) if it's in an inner loop of something that only uses 4 instructions to execute. Commented Nov 28, 2014 at 14:53
6

Global variables are allocated and initialized before the main function starts, while local variables are generated on the stack as the instance of the program runs.

Not the answer you're looking for? Browse other questions tagged or ask your own question.