Static Storage Class
So far, we have learned about the auto and register storage classes. In this tutorial, we will learn about the static storage class, its features, and why it is needed.
So far, we have learned about the auto and register storage classes. In this tutorial, we will learn about the static storage class, its features, and why it is needed.
The syntax to declare a static variable is as follows:
static data_type variable_name;
Example:
static int var = 10;
Following are the features of a static storage class:
The static storage class is useful when you need to access the value of a variable after the code block in which it is defined has been executed. This is because variables with static storage class are allocated memory in the static storage area, which is a reserved area of memory that persists for the lifetime of the program. This means that the value of a variable with static storage class can be accessed even after the code block in which it is defined has been executed.
To better understand the concept, consider the following program:
#include <stdio.h>
void fun()
{
int i = 0;
i = i + 1;
printf("%d", i);
}
int main() {
fun();
fun();
fun();
return 0;
}
Let’s understand the above program.
Inside the main()
function, the function fun()
is called thrice. The flow of execution is as follows:
First call to fun()
:
Second call to fun()
:
Third call to fun()
:
So, the final output is
Let’s analyze what has happened so far. The variable i inside the fun()
function is an ordinary auto variable whose scope and lifetime are limited to the fun()
function’s block. The fun()
function is called three times inside the main()
function. This means that the code inside the fun()
function will be executed three times. Because i is an automatic variable, a new variable i is defined and initialized to 0 each time the function is called. As a result, 1 will be printed to the screen after each increment.
But what if we want to retrieve the old value and increment it instead of redefining and reinitializing the variable? This is where the static storage class comes in.
Consider the following updated program with the static variable definition within the fun()
function:
#include <stdio.h>
void fun()
{
static int i;
i = i + 1;
printf("%d", i);
}
int main() {
fun();
fun();
fun();
return 0;
}
Output:
By simply adding the keyword “static” before the variable i, the output magically becomes 123. But how does this happen? Let’s analyze the flow of execution of the above program:
First call to fun()
:
Second call to fun()
:
Third call to fun()
:
There are two important things to understand about static variables. First, the value of a static variable is stored in static storage and remains intact even when the control goes out of the block where it is defined. Second, static variables are defined and initialized only once. This means that when the function fun()
is called the second or third time, the variable i will be able to retrieve its old value instead of being initialized to zero. Static variables are useful when you need to make changes to the old value of a variable or when you want a variable to remain in memory even after the block execution is completed.
You may have noticed that the static variable i is not explicitly initialized to zero, but it still takes the value zero for the first time. This is because the default value of a static variable is zero, unlike automatic or register variables, which have garbage values.
Leave a comment