Static variable in C is a special variable that is stored in the data segment unlike the default automatic variable that is stored in stack. A static variable can be initialized by using keyword static before variable name. As the name indicates, the static variables retain their value as long as the program executes.
This is useful if we want to isolate pieces of a program to prevent accidental changes in global variables. You can declare a static variable by using the static storage class access specifier. For example,
static int a = 5;
A static variable behaves in a different manner depending upon whether it is a global variable or a local variable. A static global variable is same as an ordinary global variable except that it cannot be accessed by other files in the same program / project even with the use of keyword extern. A static local variable is different from local variable. It is initialized only once no matter how many times that function in which it resides is called. It may be used as a count variable.
Static variables can be of two types depending on the placement of the declaration.
• Static variables with block scope.
• Static variables with file scope.
Static variables with block scope are those which are declared inside a function. The scope of these types of static variables is the same as that of auto variables, i.e., they are accessible only within the function they are declared and cannot be accessed from outside. However, unlike auto variables, they are not created and destroyed each time the function they are declared is called; instead, they are created once, and they retain their value throughout the program’s life.
The last value is stored in the variable when the function is exited available to the function the next time it is called. Also, static variables can be initialized with constants or constant expression. If not initialized, by default, they are initialized to 0. Static variables are frequently used in situations in which you want to retain information between function calls such as several times the function is called etc.:
#include<stdio.h> #include<conio.h> void func_static (); void main() { int i; for(i=1;i<=4;i++) func_static(); getch(); } void func_static (){ static int num = 0; printf( "Static variable num num++; %d\n",num); num++; }
Explanation: On execution of this program, the function func_static() is called four times from within main () as the loop is executed four times. In the function func_static(),num is astatic variable initialized to 0. This initialization is done once when the program starts and is not reinitialized on re-encountering the declaration in the subsequent calls to the function.
The function func_static() increments the value of num just before returning control back to main(). Since num is a static variable, memory for num remains allocated between function calls as long as the program executes. So the value that the num has when leaving the func_static() is retained and displayed when
the function is next called, as clear from the output.
Static variables with file scope are those which are declared outside of all the functions. Once these types of variables are declared, it exists until the program is declared has finished executing. They can only be accessible in the file in which they are declared and not across other files in the multifile program. Consider the following code segment,
static int x; main() { /*has access to x*/ void func1() { /*has access to x*/ } }
In this code segment, the function main() and func1() both have access to x, but functions in other files don’t.