Static Storage Class
The static variable may be of an auto and global type depending upon where it is declared. If declared outside the function of the body it will be static global. In case, if it is declared in the particular block, it is treated as the local or auto variable. The contents stored in these variables remain constant throughout the program execution. A static variable is initialized only once, it is never reinitialized. The value of static variable persists at each call and the last change made in the value of static variable remains throughout the program execution. The static variable is also used to count how many times a function is called.
S.No.
|
Features of Static Variable
|
Meaning
|
1.
|
Storage or location of variable
|
Memory
|
2.
|
Initial or default value
|
Zero
|
3.
|
Scope of the variable
|
Local and Global both depends upon the location
|
4.
|
Life of the variable
|
Value of the variable persists between different function calls.
|
Example of the static storage class
main()
{
count();
count();
count();
count();
}
count()
{
static int num=1;
printf("%d\t",num);
num++;
}
Output: 1 2 3 4
Explanation: The count function is called four times so it will print the value from 1 to 4 because the value of static variable remains persistent.