Storage Classes in C

Posted in /  

Storage Classes in C
vinaykhatri

Vinay Khatri
Last updated on December 21, 2024

    When we declare a variable in C, with its declaration, we can tell some of its properties such as variable name, variable data type, and variable size in the memory. Apart from these three properties, a variable also possesses some other properties such as variable default value, variable storage, variable scope, and variable life. With the variable declaration, we cannot tell all the preparties of a variable, but with the help of storage classes in C, we can.

    What are Storage Classes in C?

    Do not confuse it with the class data structure. A storage class means storage classification of a variable. It is a concept in C that can reveal all the unknown properties of a variable, such as variable default value, variable storage, variable scope, and variable life.

    Variable Properties Descriptions
    Default value For e.g. int x; What would be the default value of the variable x if we have not defined it during the variable declaration? Would it be a garbage value or something else?
    Variable storage The variable could be stored in two memory locations. It could be CPU main memory (RAM) or CPU registers.
    Variable scope The scope of the variable defines the visibility of a variable in the whole program.
    Variable life What would be the life cycle of the variable? The life period of the variable from the creation of the variable to the destruction of the variable.

    Storage Classes in C

    With the help of storage classes, we can classify all these above properties of a variable at the time of its declaration.

    Storage Class Specifiers

    There are four types of storage classes in C that are known as storage class specifiers.

    1. Automatic (auto)
    2. Register (register)
    3. External (extern)
    4. Static (static)

    Syntax of Class Storage Specifier in C

    <Class Storage Specifier> <data type> <variable name>;

    Example:

    auto int x;
    

    Note: Storage class specifiers are optional. If we do not define them, then, by default, our variable will be considered as an auto storage class specifier.

    Storage Class Specifier Keyword Storage Default Value Scope Life
    Automatic auto RAM Garbage Limited to the block Limited to the block execution
    Register register The Register Memory Garbage Limited to the block Limited to the block execution
    Static static RAM 0 Limited to the block Throughout the complete program
    External extern RAM 0 Global Throughout the complete program

    1) Automatic Storage Class Specifier

    When we declare a variable with the auto keyword storage class specifier, it belongs to the automatic storage class. If we do not mention any class specifier to a variable, it would also fall in the automatic storage class. The variables of the automatic storage class use the RAM memory to execute, and they have garbage value by default if we have defined no value. The life of an automatic class storage variable is limited to the execution block of that function in which it has been defined. Once the function execution is completed, the compiler destroys the variable.

    Code Example:

    #include<stdio.h>
    void main()
    {
     auto int x; // here x will have a garbage value
     {    auto int x = 1;
          {   auto int x = 2;
                      printf("%d\n",x);
          }
          printf("%d\n",x);
     }
    printf("%d\n",x);
    getch();
    }

    Output:

    2
    1
    -23234
    

    2) Register Storage Class Specifier

    When we want a variable to follow the property of a register storage class, then we use the register keyword before the variable declaration. The register class specifier is quite similar to the auto class specifier. The only difference is that the register storage class variable uses the register memory instead of RAM. Due to using the register memory, the register variable acts faster than auto variables. The scope and life of a register variable are limited to the block function.

    Code Example:

    #include<stdio.h>
    void main()
    {
    auto int x; // here x will have a garbage value
     {    auto int x = 1;
          {   auto int x = 2;
                      printf("%d\n",x);
          }
          printf("%d\n",x);
     }
    printf("%d\n",x);
    getch();
    }

    Output:

    2 1 -28760

    3) Static Storage Class Specifier

    We use the static keyword to make a variable, and it belongs to the static storage class. If a variable becomes static by nature, its values by default become zero. Though its scope will be limited to the function blocks, its value is not destroyed even after the function execution. It uses the RAM memory to store the variable value. We often use the static storage class specifier when we want the user-defined function local variables not to destroy their value after each function call.

    Code Example:

    #include<stdio.h>
    void func1()
    {
       static int x;
       static int count = 1;
       x+=2;
       printf("The value of x when function call %d time is %d\n",count,x);
       count+=1;
    }
    
    void main()
    {
       static i; //by default the value of i will become 0
       printf("the value of i is %d\n",i);
       func1();
       func1();
       getch();
    }

    Output:

    the value of i is 0
    The value of x when function call 1 time is 2
    The value of x when function call 1 time is 4

    4) External Storage Class Specifier

    External storage is quite similar to the static storage class specifier. The only difference is the scope of the variable is global, which means every function can access this variable. We do not use the extern keyword to create a variable. We just use it to refer to the global variable in the function. First, we declare a variable globally, and to access that variable, we use the extern keyword. However, we can access a global variable without using the extern keyword. Then what is the use of the extern keyword at all? We can only use the global variable in our function if it is declared before the function declaration. What if we use to access a global variable that is declared after the function declaration? This will cause an error.

    Code Example:

    #include<stdio.h>
    void main()
    {
       extern int i;
       printf("the value of i is %d\n",i);
       getch();
    }
    int i=20;

    Output:

    The value of i is 20

    Note: If you try to execute the above program without the extern keyword, it will print garbage value for i.

    Conclusion

    Storage classes in C describe all the features or properties of variables, such as default value, variable storage, variable scope, and variable life. Alternatively, we can say that storage classes in C determine the visibility, initial value, and lifetime of a variable. We have explained all four types of storage classes, namely automatic, register, static, and external, along with their examples. We hope this article has helped you have enough clarity on what a storage class in C is and its different types. You can post your queries or suggestions in the comments section below, if any.

    People are also reading:

    Leave a Comment on this Post

    0 Comments