Storage Classes in C programming language

In order to define a variable we not only need a data type but also a storage class. Storage class defines the visibility(scope) and lifetime of a variable in a C program. It also determines whether the given variable shall be stored in the memory or the CPU registers.

In case a variable hasn’t been assigned a storage class, the compiler puts a storage class automatically depending on the context of variable usage.

A storage class also hints at the default initial value of the variable(if not assigned).

Here are the four storage classes available in C programming language:

  • Automatic Storage Class
  • Register Storage Class
  • Static Storage Class
  • External Storage Class

Automatic Storage Class

A variable defined with an automatic storage class has the following features:

  • It is stored in the memory.
  • Its scope is limited to the block where it is defined.
  • The initial value that it contains(if not already assigned) is any garbage value.
  • It remains active till the control is in the block where the variable is declared.
  • The keyword used to define a variable with the automatic storage class is ‘auto’.

The following program will help illustrate the use of the automatic storage class:


#include<stdio.h>
int main()
{
  auto int a=1,b;
  printf("%d\n",b);
  {
    auto int a=5;
    printf("%d\n",a);
  }
    printf("%d",a);
    return 0;
}


Output:-

8
5
1

In the given program, the first auto with the variables a and b assigns the given value 1 to a and a garbage value(here 8) to b. Hence, we get the output of 8. The printf() present in the innermost block prints the value of the closest a(i.e., a=5). Since the two a’s are in different blocks they are totally different variables. And at last the printf() prints the value of the the first a(i.e., a=1) because a=5 is not accessible (it is present in the inner block).


Register Storage Class

A variable defined with a register storage class has the following features:

  • It is stored in the CPU registers.
  • Its scope is limited to the block where it is defined.
  • The initial value that it contains(if not already assigned) is any garbage value.
  • It remains active till the control is in the block where the variable is declared.
  • The keyword used to define a variable with the register storage class is ‘register’.

A register storage class works in a very similar manner to the automatic storage class except that it stores the variable in the CPU memory. This makes it easier to access the variable at any point in the program. Therefore, if we use a variable that needs to be accessed at many places in the program it is better to use the register storage class to declare the variable. In case, the CPU registers are busy doing some other work(CPU registers are quite limited), then the variable is treated as an auto variable.

Also, in case the microprocessor has 16-bit registers then it cannot store double or float variables because their size is 4 and 8 bytes respectively. Thus, the variable shall again be treated as auto variable.


Static Storage Class

A variable defined with a static storage class has the following features:

  • It is stored in the memory.
  • Its scope is limited to the block where it is defined.
  • The initial value that it contains(if not already assigned) is zero.
  • It remains active between different function calls.
  • The keyword used to define a variable with the static storage class is ‘static’.

The following program will help illustrate the use of the static storage class and how it is different from the automatic storage class:


#include <stdio.h>
void change()
{
    auto int a=1;
    static int b=1;
    printf("%d   %d\n",a,b);
    a++; b++;

}
void main()
{
    static int c;
    int i=1;
    printf("%d\n",c);
    while(i<=5)
    {
        change();
        i++;
    }
}


Output:-

0
1  1
1  2
1  3
1  4
1  5

Here we see that the value of the variable b which is declared as static changes but the value of variable a which is declared as auto does not change. This is because static does not lose its value between function calls but auto variable changes its value back to 1 because the changed value is valid only inside the block where it is changed. Once it goes back to the change() function it again starts with the value 1. Also we see here that we get a value 0 when c is printed because the initial value for a static variable(if not already assigned) is taken as zero.


External Storage Class

A variable defined with a external storage class has the following features:

  • It is stored in the memory.
  • Its scope is global.
  • The initial value that it contains(if not already assigned) is zero.
  • It remains active till the end of the program. Not accessible to other files.
  • The keyword used to define a variable with the external storage class is ‘extern’.
  • When declared outside all the functions in the program its declaration does need to be preceded by the keyword.

The following program will help illustrate the use of the external storage class


#include<stdio.h>

int a=3;
void func()
{
    extern int b;
    printf("%d  %d\n",a,b);
}

int b=5;
void main()
{
    printf("%d \n",a);
    func();
}



Output:-

3
3  5

Here, the variable ‘a’ is declared outside all the functions hence it is accessible throughout the program. The statement extern int b is the declaration for the variable ‘b’ before the variable is being printed. It has been defined as 5 outside the function which makes it accessible to the given function. Also a variable can be declared many times according to the programmers’ choice but it can be defined only once.

So that’s all for this tutorial. Hope this helps and you like the tutorial. Do ask for any queries in the comment box and provide your valuable feedback. 

Do not forget to SHARE and SUBSCRIBE.

Keep Coding!! Happy Coding!! 🙂