I\'ve read that static variables are used inside function when one doesn\'t want the variable value to change/initialize each time the function is called. But what about def
... change/initialize each time the function is called
You use the words "change" and "initialize" as though they were the same, but they aren't
void f(void) {
static int a = 0;
a++; // changed!
printf("%d\n", a);
}
int main(void) {
f(); f();
}
/*
# 1
# 2
*/
When at file-scope (outside functions) static
does not mean "const" as in "static value", but it means that the identifier can only be referred to in that translation unit.
So your first m
without const
can still be changed. Only const
guards against changes. But if you omit static
then if you link in a library or another object file that has the same non-static identifier at file-scope you will get conflicts at link-time.