What is the Static Global Variable Purpose?
I know that the variable that is declared static only has scope in the
file in which it is declared where as the variable declared without static
can be accessed from other files using an extern declaration.
but Is it true that the static "Global" is to make the variable retain the
last assigned value. Like the Static Local Purpose ?
#include <stdio.h>
void func() {
static int x = 0; // x is initialized only once across three calls of func()
printf("%d\n", x); // outputs the value of x
x = x + 1;
}
int main(int argc, char *argv[]) {
func(); // prints 0
func(); // prints 1
func(); // prints 2
return 0;
}
No comments:
Post a Comment