Storage Classes in C

Storage class is a characteristics of possession by a variable in C. The storage class of a variable determines the nature of the variable. Whenever you declare a variable, specify the storage class and data type of variable. C language supports four types of storage class 1)auto 2)register 3)static 4)extern

 

Auto Storage Class..

The variable that is declared within a function and having block scope, are under auto storage class. Any variable declared within a function whose storage class is not specified by default, assumes to be auto.

Example….

#include<conio.h>

#include<stdio.h>

void main()

{

       void add5();

       clrscr();

      add5();

      add5();

     add5();

    getch();

}

void add5()

   {

     auto int x=20;

     x=x+5;

    printf("x=%d\n",x);

   }

Auto Storage class in C

Explanation…

Variable x is having auto storage class, defined in function add5(). When a call is made to function add, it initializes the value x=20 and then add 5 to it and prints the result. As a new call is made to same function it again initializes to 20 nd loses the previous value i.e 25.

Register Storage Class..

Registers are special storage areas the CPU of computer. Whenever some arithmetic or logical operation is carried out, the data is transferred from computer’s memory to register and the operation is carried out. After the computation, result is sent back to memory. This process is repeated many times during the execution of program. If you specify the storage class of a variable, then its default storage location is CPU register.

Example…

#include<stdio.h>

#include<conio.h>

void main()

{

     register int i=0;

     register int j;

     clrscr();

     for(i=0;i<5;i++)

     {

       printf("%d\n",i);

     }

   printf("value of j=%d",j);

   getch();

}

Register Storage class in C

Explanation…

In above code, value of j is not initialized so, it takes a garbage value. When register storage is specified with loop counter it enhances the performance of the program.

Static Storage Class..

The static variable can be applied both to local as well as to global variable. These have the scope local in the block in which it is declared but it has global life time i.e its value persists between different function calls.

Example….

#include<stdio.h>

#include<conio.h>

void main()

{

     void add5();

     static int y;

     clrscr();

     printf("y=%d",y);

    add5();

    add5();

    add5();

    getch();

}

void add5()

{

   static int x=20;

   x=x+5;

   printf("x=%d \n",x);

}

Static Storage in C

Explanation…

In this program the variable x is declared as static and it is initialized to 20 During the first call to the function its value is incremented by 5. When next time add5 function is called, its value is not reinitialized, it retains old value x=25 and add upto new value become x=30. Similarly in the third call to the function, the value of x is 35 y is not initialized in above code so it takes value 0 by default.

Extern Storage Class..

A variable declared in the global declaration section has, by default, extern storage class.Scope of the external variable is throughout the program i.e it can be accessed from any function that falls within its scope.

Example..

#include<stdio.h>

#include<conio.h>

int x=20;

void main()

{

     void add5();

     void decrement();

     clrscr();

     add5();

     add5();

   add5();

   decrement();

   decrement();

   getch();

}

void add5()

{

   x=x+5;

   printf("x=%d \n",x);

}

void decrement()

{

     int y;

     y=x--;

   printf("y=%d\n",y);

}

Extern Storage in C

Explanation..

In above example x is declared in the beginning of the program before main() i.e its storage class is extern, x is initialized to the value 20, every time function call is made, its value is incremented by 5, In the function decrement, value of x is decremented and result is stored in y, it demonstrates that extern variables have global lifetime.

The extern storage class cannot be used in the parameter declaration in the function prototype.

About Us

Rekha SetiaRekha Setia is a passionate blogger of Extra Computer Notes. if you have any ideas or any request me @