C Programming

Storage Classes in C

Storage classes are a very important topic in C. If we have developed our concept in C, especially about the life and scope of any variable, we must know about storage classes in C. In Dos-based architecture or 16-bit architecture like Turbo C++ 3. 0, it takes the memory of 2 bytes. But in 32-bit or 64-bit architecture like Dev C++, Code blocks and integers take the memory of 4 bytes.

Three Properties of Variables

  1. Name of variable.
  2. Size of the memory block.
  3. Type of content.

Other Properties of Variable

Default value, Scope, Life, Storage.

Default value: If we do not initialize any value to a variable at the time of variable declaration, then what is the value of the variable x? Then the value of x is called the default value, which is another characteristic of the variable.

Storage: Storage means where memory is allocated for a variable, either RAM or in the CPU register.

Scope: The limitations or areas where we can access a variable

Life: The span between creating and destroying a variable is called Life. These 4 properties we can realize at the time of declaring a variable.

There are 4 types of storage classes in the declaration statement.

  1. Automatic
  2. Register
  3. Static
  4. External
Storage class Keyword Default value Storage Scope Life
1. Automatic auto Garbage RAM Limited to the blocks where it is declared Till the execution of the block in which it is declared
2. Register register Garbage register same same
3. Static static 0( zero ) RAM Same Till the end of the program
4. Extern extern 0( zero ) RAM Global same

Automatic Storage Class Program

Programming Example1

#include<stdio.h>

int main()
{
    // auto variable by default ;
    int x=5;               
    printf("%d\n",x);

    {
       int x=2;
       printf("%d\n",x);
    }

    printf("%d\n",x);
    return 0;
}

Output

Explanation

With the arrow symbol, we can write an auto keyword to make the variable automatic. If we write nothing there, the variable is an automatic type (by default). The rule is to give higher priority to the local variable if the name of two variables inside the function is the same.

Register Storage Class Program

Programming Example 2

#include<stdio.h>

int main()
{
        // register keyword is used ;
    register int x=4;          
    int y;
    y=x++;
    x--;
    y=x+5;

    printf("%d %d",x,y);
    return 0;
}

Output

Explanation

If in a program, some variables are used repeatedly, in that case, we store the variable x inside of a CPU register instead of RAM. It reduces the processing time of our program, but it is applicable only int and char mean small memory black. If Register is not available, it automatically converts the register storage class to an auto class.

Static Storage Class Program

Programming Example 3

#include<stdio.h>

void f1();
int main()
{
    f1();
    f1();
    return 0;
}
void f1()
{
    int i=0;
    i++;
    printf("i=%d\n",i);
}

Output

Explanation

Here we get result two 1 because we declare the variable by default Auto.

Programming Example 4

#include<stdio.h>

void f1();
int main()
{
    f1();
    f1();
    return 0;
}
void f1()
{
    // static variable is declared ;
    static int i=0;    
    i++;
    printf("i=%d\n",i);
}

Output

Explanation

As it is a static variable, this variable is not destroyed from the body of f1. So, when f1() is called 2nd time, the output will be 2.

External Storage Class

Programming Example 5

#include<stdio.h>

int x ;
int main()
{
   // Extern is declared ;
   extern int x ;      

    printf("x=%d",x);
    void f1( void ) ;
    f1();
    printf("x=%d",x);
    return 0;
}

void f1()
{
    x++;
    printf("x=%d",x);
}

Output

Explanation

As it is a global variable, the variable is accessible from anywhere in the program, and its life is throughout the program.1st printf() function print the value of x = 0, as it’s default value = 0, then f1() is calling, then x is incremented to 1, and print the value 1. Then control again goes to the main () function after f1() and prints the value of x again 1.

Programming Example 6

#include<stdio.h>

int x;
void f1();
int main()
{
    extern int x;
    printf("x=%d\n",x);
    f1();
    printf("x=%d\n",x);
    return 0;
}

void f1()
{
    int x=5;
    x++;
    printf("x=%d\n",x);
}

Output

Explanation

As the variable x in the function f1() is an automatic variable, it is accessible only in the f1().So, here printf() function print the value of x = 5 but in main() function, x a is treated as external variable, because x is declared as global. So, it prints the value of x = 0.

Where do we Declare the Extern Keyword?

An external variable is declared outside any function. But it’s denoted that variable is extern inside the main function as shown in this example, or it is denoted anywhere in the program.

Conclusion

From the above-mentioned concept of C language storage classes, it is clear to the reader that whenever we manage data through software, we need some variable or storage space to store the data. We now have a clear idea from this article about how the data is to be stored in a variable.

About the author

Bamdeb Ghosh

Bamdeb Ghosh is having hands-on experience in Wireless networking domain.He's an expert in Wireshark capture analysis on Wireless or Wired Networking along with knowledge of Android, Bluetooth, Linux commands and python. Follow his site: wifisharks.com