What is the Storage Classes in C Language with Free Notes

Storage Classes

Storage Classes – Each and every variables declared in C contains not only its data type but also has a storage classes specified with it. If we do not specify storage class of a variable, the compiler will assume its storage class as default i.e. automatic.

The storage class of a variable tells us about (characteristics of Storage Class).

  1. Storage place of the variable i.e. Memory of CPU register.
  2. The initial value of the variable, it it is not specified in the program.
  3. The scope or the visibility of the variable.
  4. The lifetime of the variable i.e. how long the variable exists.

There are four types of Storage Classes in C, All of them has the four characteristics, according to which they can be differentiated.

  1. Automatic Storage Class (with Specifier auto)
  2. Register Storage Class (with Specifier register)
  3. Static Storage Class (with specifier static)
  4. External Storage Class (with Specifier extern)

1. Automatic Storage Class (Local Variables)

The characteristics of a automatic storage class variables are:

Storage Memory
Default Initial ValueGarbage value i.e. an unpredictable value.
Scope or Visibility Local or visible to the block in which variable is declared e.g. if the variables is declared in function then it is only visible to that function.
Life TimeIt retains its value till the control remains in the block in which the variable is declared. e.g. if the variables is declared in a function, it will retain its value till the control is in that function.

Example: Demonstrate of auto storage class with default value,

void main (0
{
    auto int i,j =5;
    int k, l = 10;
       printf("\n value of i = %d \n value of j = %d", i,j);
       printf("\n value of k = %d \n value of l = %d", k,l);
}

Output

value of i = 2009
value of j = 5
value of k = 1005
value of l = 10

The default storage class is auto. So whether or not it is defined it will work as same.

The value of i and k are garbage values (unpredictable values) because they are not defined in the program.

Example: Demonstration of scope or visibility and lifetime of auto storage class.

void main(0
{
     auto int x = 5;                                 //block 3
      {
         auto int x = 10:                     //block 2
            {
               auto int x = 15;
                {
                  printf("\n%d", x);    //block 1
                }
         printf("%d", x);                 
            }
            printf("%d", x);
            }
        }

Output

15 10 5
  • As its scope is local to the block in which it is defined so block 1 will print 15, block 2 will print 10 and block 3 will print 5.
  • These three variables x are treated as different for three blocks.
  • The life of x will persist as its value 15 in block 1 and then it expires.
  • After that another x whose value is 10 will persist in block 2 and then expires.
  • Similarly last x whose value is 5 will persist in block 3.

2. Register Storage Class

The characteristics of register storage class variables are:

StorageCPU register
Default Initial value Garbage Value
Scope Local to the block in which variable is declared (same as automatic)
LifetimeIt retains its value till the control remains in the block in which the variable is declared. (same as automatic)
  • A variable is nothing but memory location in the main memory of the computer and CPU has to access the main memory to manipulate the variables.
  • This activity slows down the execution of the program because main memory is about 10 times slower than the CPU.
  • Since a number of register are available in CPU itself and all of them are not used all the time, a few of them can be used to store some frequently used variables so that execution becomes faster.
  • C allows some variables to be declared as register variables. However it is limited in numbers and varies from compiler to compiler.
  • The keyword register can be prefixed to an ordinary variable foo declaring it as a register variable.
e.g. - register int i;

  • Normally registers are of 16 bit size so we can not use register with float, double, array or structure. But if we use them a a register, the compiler will automatically convert them into automatic.
  • We use frequently used variable e.g. loop counters as a register storage class. Because speed of the Program, increases by using a variable as register storage class.
e.g.  void main()
        {
           register int i;
         for (i = 1; i< 100: i++)
            printf ("\n%d", i);
        }

3. Static Storage Class

The characteristics of a variable defined to have static storage class are:

Storage Memory
Default Initial value Zero
Scope or Visibility Local to the block in which the variable is declared (same as auto storage class)
Lifetime It retains its value between different function calls i.e. when a function is called for the first time, static variable is created with initial value zero, and in subsequent calls it retains its present value.
  • If one desires that the local variable should retains its value even after the execution of the function in which it is declared, then the variables must be declared as static.
  • The value of the static variables is not destroyed when the function terminates.
  • The value is retained and is available to the user even if the control returns back to the function again.
C Language Notes

Example: Demonstration of static Storage class

#include <stdio.h>
#include <conio.h>
void main()
    {
      int i, r;
      clrscr();
      for (i = 1; i<=5; i++)
            {
                  r = tot(i);
                  printf("\t%d", r);
            }
            getch;
      }
        tot (intx)
            {
            static int s=0;
            s = s+x;
            return (s);
      }
  • The function main() calls the function tot() five times, each time sending the value of i varying from 1 to 5.
  • In the function value is added to s and the accumulated result is returned.
  • Once the program is executed, the following output is produced.
1    3    6     10    15

i.e. the contents of variables are returned in each function call.

Example: Write the output of the following,

#include <stdio.h>
#include <conio.h>
static int i = 100;
void abc()
    {
      static int i = 10;
         printf("\n First = %d", r);
    }
void main ()
    {
      static int i = 5;
      clrscr();
      abc();
           printf("\t second %d", i);
            getch;
      }

Output

The output is 
First = 10 second = 5

4. External Storage Class

The characteristics of a variables defined to have extern storage class are:

Storage Memory
Default Initial ValueZero
Scope or Visibility Global
LifetimeIt retains its value throughout the program. So the life time is as long as the program’s execution does not come to an end.
  • External variables are declared outside all functions i.e. at the beginning of the program.
  • When the size of the program becomes very large then it has to be divided into parts and each part is stored in a separate function.
  • Global variables should be available to all the functions with the help of extern specifier.

Example 1: Demonstration of external storage class

extern int rate
void main ()
{
     --
     --
}
fun1 ()
{
     --
}
compute()
{
     int tot, net;
     --
     net = tot *rate;
}

It can be noted that the global variables rate has been defined in the beginning of the file. So it is available globally i.e. all the functions like fun1() and compute().

Note: It is not necessary to mention keyword extern with external variables because they are declared before main().

e.g.  int rate;  //No need to mention extren int rate
void main()
{ 
     -
     -
}

Example 2 : What will be the output of the following:

int i;   //Global variable
void main()
{
   printf("\n i = %d", i );
   increment();
   decrement();
}
increment()
{   
   i++;
   printf("\n i =%d", i);
}
decrement()
{
  i--;
  printf("\n i =%d", i);
}

Output

i = 0 
i = 1
i = 0

Which Storage Class to use When ???

The programmer can decide as his/her own that which storage class has to be used and when, according to the requirements in a program.

He can use appropriate Storage Class by following the views for usage of different storage classes in different situtations.

  1. Economies the memory space requirements consumed by the variables.
  2. Improve the speed of execution of the program.

The various rules that govern which storage class to use and when are:

  1. Use register Storage Classes for only those variables which are being used very often e.g. loop counter variable.
  2. Use static Storage Classes only if you want that the value of the variables to persist between various function calls.
  3. Use extern Storage Classes for only those variables which are being used by almost all the functions present in the program.
  4. In all other cases, use auto type of storage class.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top