C - Storage Classes


  • Storage classes define the life of variable that is how long a variable would be active in the program.
  • Storage classes also define the scope and storage area of a variable.
  • It also determines the initial value of a variable if it is not initialized.
  • In C Language storage classes can be categorized into four parts as follows:
    • auto
    • extern
    • static
    • extern

Properties of All storage class

storage-class

auto storage class

  • auto storage class is default storage class.
  • A variable declared in a function or block without storage class name, by default is an automatic variable.
  • These automatic variables are local to that function or block in which they are defined. Outside that block or function the content or existence of the automatic variable get vanish.
  • Here is an example of automatic variable:
Example
#include<stdio.h>

#include<conio.h>

void fun(void);

int main()

{

int a=10;//by default auto storage class

fun();

printf("\na=%d",a);

getch();

return 0;

}

void fun()

{

int a=20;//by default auto storage class

printf("\na=%d",a);

}

The output of the above program is as follows:

Output
a=20

a=10

Extern storage class

  • The variables that are declared with the extern storage class are known as external variables or global variable.
  • External variables are available to all the functions present in the program. These variables are declared outside the function body.
  • If in any case auto and external variable are declared with the same name then priority is given to the auto variable and external variable is hidden in that case.
  • Here is an example of external variable:
Example
#include<stdio.h>

#include<conio.h>

void fun(void);

extern int a=5;//external variable

int main()

{

fun();

printf("\nIn main() a=%d",a);

getch();

return 0;

}

void fun()

{

printf("\nIn fun() a=%d",a);

}

The output of the above program is as follows:

Output
In fun() a=5

In main() a=5

Static storage class

  • The variables that are declared with the static storage class are known as static variables.
  • The default value of static variable is Null or zero.
  • Static variable may be automatic or external. If it is declared outside the function body it will be static global and if it is declared inside the function body it will be static auto variable.
  • Here is an example of static variable:
Example
#include<stdio.h>

#include<conio.h>

void fun(void);

int main()

{

static int i;//static variable

printf("\ni=%d",i);//by-default value is 0

getch();

return 0;

The output of the above program is as follows:

Output
i=0

Register storage class

  • The variable declared with the register storage class is called as Register variable.
  • The register variables are the local variables that stored in the CPU register instead of memory.
  • The variable stored in CPU register can access faster than the variable stored in memory but we cannot declare more variables with register keyword because CPU registers are limited in number.
  • Here is an example of Register variable:
Example
#include<stdio.h>

#include<conio.h>

int main()

{

register int a=3;//register variable

for(;a<=5;a++)

printf("\na=%d",a);

getch();

return 0;

The output of the above program is as follows:

Output
a=3

a=4

a=5