Though the variable with different local variable, global variable, and longevity has been taken up
Actually, it is these two intermediate existence, and the variable with a changeable feature exists.
In static (static) and applying before the name of the type when it declares a variable in the function,
A static local variable can be declared.
The following program is an example of having declared a static local variable.
The execution result of this program becomes as follows.#include <stdio.h> int countfunc(void); int main(void) { countfunc(); countfunc(); countfunc(); return 0; } int countfunc(void) { static int count; /* static local variable */ Count++; printf("%d\n",count); return count; }
One value has increased at each call though it declared in the function.
1
2
3
However, variable count is a local variable because it is declared in the function.
Actually, if variable count is used in the main function, it becomes an error.
This is a feature of a static local variable.
Being possible to use it : only in the declared function because it is declared in the function.
The value remains until the program ends.
Moreover, it is automatically initialized by 0 even if it doesn't especially initialize it.
Because initialization is done only once in the beginning
For instance, it is possible to count when initializing it as follows.
This variable is used to remember the value when the function was called before.static int count = 0; /* Static local variable */