There are four storage class in c language.

  • Automatic storage class.
  • Register storage class.
  • Static storage class.
  • External storage class.

In c language we declare a variable of different types like int a, float b,double c etc. Here the variables a,b,and c are nothing but these are a fixed number of locations reserved in  CPU memory (RAM)  area under the specific name. The storage class controls the life and scope of  these  c variables. The computer has two types of memory location and these location are accessible for a c program. These locations are

(1)  Memory

(2) CPU Register

The storage class also do

  • The default initial value of c variables.
  • The scope of the variables.
  • life of the variables.

Automatic storage class

#include<stdio.h>
int main()
{
       auto  int a; /* uninitialize*/
       printf(“the value of a = %d\n”,a);
return 0;
}

In The above example the statement

auto int a;

a is c variable name and int is type specifier and auto is storage class specifier.

By default the variable declared inside a function block is assumed to be auto. These variables are called local variables ,even without auto keyword they are automatic storage class. The life of automatic storage class is local to function.

Register Storage class

  • The keyword register is used to declare a local variable to be stored in cpu register.
  • & operator can not be used on register class variables.
  • The register is nearest to cpu processor hence access is faster compare to non register class variables.

Storage classes in c

  • register is only allowed inside functions, so its life or scope  starts from its declaration and ends at the block where that declaration occurs.

Static storage class

A static variable is declared with keyword static like static int a.

  • The initial value of a static variable is 0 (zero) or NULL in case of pointer.
#include <stdio.h>
      static char c;
      static int i;
      static float f;
      static char *name;

int main(){

   printf("c=%d i=%d f=%f name=%s",c,i,f,name);
   printf("\n");
return 0;
}

Output:

c=0 i=0 f=0.000000 name=(null)
  • We can declare same static variable declared many times but we can initialize at only one time.
  • A static variable declared inside a block i.e locally then its visibility will be within  that block where it has been declared.
 1 #include<stdio.h>
2 int main(){
3
4     {  // block start
5
6          static int a=20;
7          printf("%d \n",a);
8
9     }     // block end
10
11 //    printf("%d \n",a);
12 // if we uncommnet the code
13 // it will not compile
14 // because a will not be vissible after line 9
15    return 0;
16 }
  • The value of static variable persists till the end of the program.
#include<stdio.h>
void sum ()
{
  static int i=0;
  i++;
  printf("%d ",i);
}

int main()
{
  sum();
  sum();
  sum();
  printf("\n");
return 0;
}
OUTPUT:
1 2 3
  • Static variable has the scope inside that file only where they are been declared.

External storage class

  • Initial value of extern storage class variable is zeor (0).
  • The life of extern storage class variable remains till the end of program.
  • The declaration of a variable as an extern means , the variable is already declared in external file.


Related Contents to follow