I'm trying to learn how static variable work in c for when they are defined in a given function. For example, when I write the following:
#include <stdio.h>
void inc() {
static int c = 0;
c++;
printf("%d\n", c);
}
int main(void) {
inc();
inc();
inc();
return 0;
}
The expected output is obviously:
1
2
3
On the first call of the function, the static variable c is defined and given the value of 0, which makes perfect sense. It is the incremented and printed. However, on the second call to inc()
why is it that the integer c is maintained and not set to zero, even though the code literally says static int c = 0;
. What mechanism in the compiler stops c from having it's value set to zero like during the first call?