Static variable inside of a function in C
There are two issues here, lifetime and scope.
The scope of variable is where the variable name can be seen. Here, x
is visible only inside function foo()
.
The lifetime of a variable is the period over which it exists. If x
were defined without the keyword static
, the lifetime would be from the entry into foo()
to the return from foo()
; so it would be re-initialized to 5 on every call.
The keyword static
acts to extend the lifetime of a variable to the lifetime of the programme; e.g. initialization occurs once and once only and then the variable retains its value - whatever it has come to be - over all future calls to foo()
.
Output: 6 7
Reason: static variable is initialised only once (unlike auto variable) and further definition of static variable would be bypassed during runtime. And if it is not initialised manually, it is initialised by value 0 automatically. So,
void foo() {
static int x = 5; // assigns value of 5 only once
x++;
printf("%d", x);
}
int main() {
foo(); // x = 6
foo(); // x = 7
return 0;
}