Although the lifetime of static
variable isn't tied to the scope where it is defined (unlike variables with automatic storage duration):
{
static int i=0;
i++;
...
{
i++; // <-- still well defined, even in nested scope
}
}
i++; // <-- undefined
it is accessible only within this scope. The compiler just checks whether the symbol i
has been defined before and it sees, that i
has not been defined within that scope (the static int i=0;
defines a variable that is accessible locally ~ compiler doesn't care about its lifetime).
In case you need it to be accessible out of its scope, you'll have to pass it out of it by reference (its address) or make it global:
static int i = 0;
...
{
i++;
}
...
i++; // <-- accessing global variable