After stumbling onto this question and reading a little more here (c++ but this issue works the same in C/C++ AFAIN) I saw no mention to what is realy happening inside the function.
void f(){
static int c = 0;
printf("%d\n",c++);
}
int main(){
int i = 10;
while(i--)
f();
return 0;
}
In this snippet, c
lifetime is the entire execution of the program, so the line static int c = 0;
has no meaning in the next calls to f()
since c
is already a defined (static) variable, and the assignment part is also obsolete (in the next calls to f()
), since it only takes place at the first time.
So, what does the compiler do? does it split f
into 2 functions - f_init, f_the_real_thing
where f_init
initializes and f_the_real_thing
prints, and calls 1 time f_init
and from that onward, only calls f_the_real_thing
?