Define the scope of static variables. The scope of a static variable is local to the block in which the variable is defined. However, the value of the static variable persists between two function calls.
Static variables in C have the scopes:
1. Static global variables declared at the top level of the C source file have the scope that they can not be visible external to the source file. The scope is limited to that file.
2. Static local variables declared within a function or a block, also known as local static variables, have the scope that, they are visible only within the block or function like local variables. The values assigned by the functions into static local variables during the first call of the function will persist / present / available until the function is invoked again.
The static variables are available to the program, not only for the function / block. It has the scope within the current compile. When static variable is declared in a function, the value of the variable is preserved , so that successive calls to that function can use the latest updated value. The static variables are initialized at compile time and kept in the executable file itself. The life time extends across the complete run of the program.
Static local variables have local scope. The difference is, storage duration. The values put into the local static variables, will still be present, when the function is invoked next time.
|