L
lovecreatesbea...
K&R 2, sec 2.4 says: If the variable in question is not automatic, the
initialization is done once only, conceptually before the program
starts executing, ... .
"Non-automatic variables are initialized before the program starts
executing." -- What does this mean? What is the name of the stage in
which the mentioned initialization is performed? Compile-time or
run-time?
In the following snippet, variables b and c are defined at line 7 & 8
but the initialization or assignment to them is not performed. The
declaration/definition and initialization/assignment are in the same
scope, why the declaration is executed but the initialization is not?
#include <stdio.h>
int main(void){
int a = 1;
switch(a){
int b = 30; /*line 7*/
int c; /*line 8*/
c = 20; /*line 9*/
case 1: /*fall-down*/
default:
printf("b: %d, c: %d\n", b, c);
}
return 0;
}
/*Result:
b: 4598440, c: 4198571*/
initialization is done once only, conceptually before the program
starts executing, ... .
"Non-automatic variables are initialized before the program starts
executing." -- What does this mean? What is the name of the stage in
which the mentioned initialization is performed? Compile-time or
run-time?
In the following snippet, variables b and c are defined at line 7 & 8
but the initialization or assignment to them is not performed. The
declaration/definition and initialization/assignment are in the same
scope, why the declaration is executed but the initialization is not?
#include <stdio.h>
int main(void){
int a = 1;
switch(a){
int b = 30; /*line 7*/
int c; /*line 8*/
c = 20; /*line 9*/
case 1: /*fall-down*/
default:
printf("b: %d, c: %d\n", b, c);
}
return 0;
}
/*Result:
b: 4598440, c: 4198571*/