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.


The Syntax

The syntax to declare a static variable is as follows:

static data_type variable_name;

Example:

static int var = 10;

Features of a Static Storage Class

Following are the features of a static storage class:

  1. Storage:  memory.
  2. Default value:  zero.
  3. Scope:  local to the block in which it is defined.  
  4. Lifetime:  till the execution of the program comes to an end.

Need of 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():

  1. Variable i is defined and initialized to zero.
  2. Variable i is incremented by 1. New value of i is 1.
  3. Value of i which is 1 is printed on the screen.
  4. Control goes out of the fun() block. Variable i is destroyed as it is an automatic variable.

Second call to fun():

  1. A new variable i is defined and initialized to zero.
  2. Variable i is incremented by 1. New value of i is 1.
  3. Value of i which is 1 is printed on the screen.
  4. Control goes out of the fun() block. Variable i is destroyed.

Third call to fun():

  1. A new variable i is defined and initialized to zero.
  2. Variable i is incremented by 1. New value of i is 1.
  3. Value of i which is 1 is printed on the screen.
  4. Control goes out of the fun() block. Variable i is destroyed.

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():

  1. Variable i (static variable) is defined and initialized to zero.
  2. Variable i is incremented by 1. New value of i is 1.
  3. Value of i which is 1 is printed on the screen.
  4. Control goes out of the fun() block. Variable i remains intact in the static storage area.

Second call to fun():

  1. The definition and initialization of variable i is skipped. As it is a static variable, the definition and initialization is done only once.
  2. Old value of i is 1 and now after increment, the new value of i is 2.
  3. Value of i which is 2 is printed on the screen.
  4. Control goes out of the fun() block. Variable i remains intact in the static storage area.

Third call to fun():

  1. The definition and initialization of variable i is skipped. As it is a static variable, the definition and initialization is done only once.
  2. Old value of i is 2 and now after increment, the new value of i is 3.
  3. Value of i which is 3 is printed on the screen.
  4. Control goes out of the fun() block. Variable i remains intact in the static storage area.

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

Leave a comment

Your email address will not be published. Required fields are marked *

Thank you for choosing to leave a comment. Please be aware that all comments are moderated in accordance with our policy. For more information, please review our comments policy. Rest assured that your email address will not be shared with anyone. Kindly refrain from using keywords in your comment. Let’s maintain a respectful atmosphere and engage in meaningful conversations.