GfG QA is closing soon... Please refer PRACTICE or GfG now on wards !!

when value of variable decided in c

Say we have static variable, automatic variable in following code:

//file.c

int g_v1;

int g_v2;

fun1(){

    static int sta_var;

    int v2;

    int v3 =4;

    return 0;

}

There are different stages of compilation- preprocessing, compiling, assembling, linking...

At which stage (default) value of these variables decided....

For static default value is 0. For automatic variable default value is junk.

asked Mar 24, 2017 by Balachander S

1 Answer

In C, the value of variable is decided when the program is loaded from hard disk to CPU.According to C standard,the uninitialized variable or object is required to have some unspecified value.This value may be taken from a register or a temporary stack location. It depends on optimization and compiler behaviors. The value may be the previous contents of the register or whatever is there at that memory location where that variable is stored.
answered Apr 10, 2017 by Ranju Kumari
...