I have the concept of static local variables down pretty well: global lifespan, local scope. Similarly, I understand automatic variables are allocated/deallocated automatically when program flow enters and leaves the variable's context.
#include <stdio.h>
void test_var(void){
static unsigned foo = 0;
unsigned bar = 0;
printf(" %u %u\n", foo++, bar++);
}
int main(void){
printf("Foo Bar\n");
printf("--- ---\n");
for(unsigned x = 0; x < 10; x++){
test_var();
}
return 0;
}
As such, the previous example behaves as expected and prints the following output:
Foo Bar
--- ---
0 0
1 0
2 0
3 0
4 0
5 0
6 0
7 0
8 0
9 0
What confuses me is how the variables behave when not initialized:
#include <stdio.h>
void test_var(void){
static unsigned foo; /* not initialized */
unsigned bar; /* not initialized */
printf(" %u %u\n", foo++, bar++);
}
int main(void){
printf("Foo Bar\n");
printf("--- ---\n");
for(unsigned x = 0; x < 3; x++){
test_var();
}
return 0;
}
Output:
Foo Bar
--- ---
0 2
1 3
2 4
3 5
4 6
5 7
6 8
7 9
8 10
9 11
So the static variable behaves as expected -- getting a default value of 0
and persisting through the function calls; but the automatic variable seems to persist as well -- although holding a garbage value, it increments in each call.
Is this occurring because the behavior is undefined in the C standard, or is there a set of rules in the standard that explain this?