What Are Storage Classes?

 In addition to the data type, each variable has one more attribute known as storage class. The proper use of storage classes makes our program efficient and fast. In large multi-file programs, the knowledge of storage classes is indispensable. We can specify the class while declaring a variable.

The general syntax is:


Storage_class data-type variable_name

There are four types of storage classes.




  1.  Automatic
  2.  External
  3.  Static
  4.  Register


 So we may write declaration statements like this.


   auto int x,y;
   register int s;
   static float d;


When the storage class specifies is not present at the declaration, compiler assumes a default storage class based up on the place of declaration.

A storage class decides about these four aspects of a variable.

   1. Lifetime -time between the creation and destruction of variables.

   2. Scope  -Locations where the variable is available for use.

   3.  Initial value-Default value taken by un initialized variables.

   4. Place of storage-Place in memory where the storage is allocated to the variable.
   

1.AUTOMATIC


 All the variable declared inside a block/function without any storage class specifier are called automatic variable.we may also use the keyword auto to declare automatic variables, although this is generally not done.

The uninitialized automatic variable initially contain garbage value.The scope of these variables is inside the function or block in which they are declared and can't be used in any other function/block.

They are named automatic since storage for them is reserved automatically each time when the control enters the function/block and are relesed automatically when the function/block terminates.

For example:

/*program to understand automatic variables*/
#include<stdio.h>
main()
{
func();
func();
func();
}
func()
{
    int x=2,y=5;
printf("x=%d,y=%d",x,y);
x++; y++;
}
output:
      
    x=2, y=5
    x=2, y=5.  
    x=2, y=5.  


Here when the function func() is called first time, the variables X and Y are created and initialized, and when the control returns to main(), these variables are destroyed. When the function func() is called for the second time, again these variable are created and initialized, and are destroyed after execution of the function. So automatic variable come into existence each time the function is executed and are destroyed when the function terminates.

Since automatic variables are known inside a function or block only,so we can have variable of same name in different functions or blocks without any conflict. For example in the following program the variable x used in different  blocks without any conflict.


/* program to understand automatic variables*/

#include<stdio.h>
main()
{


       int x=5;
      printf("x=%d\t",func());
}

func()
{
   int x=15;
   printf("x=%d \n",x);

}

Output:
x=5 x=15

Here the variable x declared inside main() is different from the variable x declared inside the function func()

2.EXTERNAL

The variable that have to be used by many fuction and different files can be declared as external variable. The initial value of an uninitialized external variable is zero.

Before studying external varible ,let us first understand the different between theit definiton and declaration . The decalaration of an external variable declare the type and name of the variable,while the definition reserve storage for the variable as well as behaves as a declaration. The keyword extern is specified in declaration but not in definition . For example the definition of an external variable salary will be written s

Float salary;

its declaration will be written as

extern float salary;

The following points will clarify the concept of defintion and declaration of an external variable.


Defintion of an external variable

 1.Defintion creates the variable  ,so memory is allocated at the time of definition
 2.There can be only one definition
 3.The variable can be initialized with the definition and initialize should  be constant
 4.The keyword extern is not specified in the defintion
 5.The defintion can be writttehn only outside funtion

Declartion of an external variable


1.The declarion does not creates the varible, it only referes to variable that has alredy been created  somewhere, so memory is not allocated at the time of declaration.
2.There can be many declaration
3.The variable cannot be initialized at the time of declaration
4.The keyword extern is always specified in the declarion
5. The declaration can be placed inside function also.

Consider this program

#Include<stdio.h>
int x=8;
main()
{

......................
..............

}

func1()
{

.........

}

func2()
{

........

}

In this program the variables x will be available to all the function ,since an external variable is active from the point of its definition till the end of a program.


Till now we had written our program in a single file. When the program is large it is written in different files and these files are compiled separately and linked together afterword to form an executable program. Now we consider a multi file program, which in three files viz . first.c ,second.c and third.c.


first.c                          second.c                                      third.c
int x=8;                        extern int x;                                 func4()
main()                          func2()                                        {
{                                  {                                                         .......
........                               .............                                   }
}                                   }                                                func5()
func1()                        func3()                                          {
{                                {                                                          .............
.........                              ...........                                       }
}                                 }



Here in the file first.c, an external variable x is defined and initialized. This variable can be used both in main() and func1() and it can be accessible to other files. In the files second.c to access this variable then we used the declaration in this file as ex tern int x;

Suppose our program consists of many files and in files first.c , we have defined many variables that may be needed by other files also. We can put a ex tern declaration for each variable in every file that needs it. Another better and practical approach is to collect all extern declaration in a header files and includes that header file in the file, which require access to those variable.


3.STATIC


There are two types of static variable
1. Local static varibles
2.Global static varibles

Local static variables


The scope of local static variable  is same as that of an automatic variable. I.E it can be used only inside the function or block in which it is defined. The Lifetime of a static variable is more than that of an automatic variable. A static varible is created at the compilation time and it remain alive till the end of program. It is not created and destroyed each time the control enters a "function/block" . Hence a static variable is created only once and its value is retained between function calls.   If it has been initialized , Then the initialization value is placed in it only once at time of creation. It is not initialized each time the function is called.

If a static variable is not explicit initialized then by default it takes initial value zero.

int x=8;
int y=x;/*valid*/
/*program to understand the use of local static varible*/
#include<stdio.h>
main()
{
 fucn();

 fucn();
 fucn();
}

fucn()
{
static int x=2,y=5;
printf("x=%d,y=%d\n",x,y);
x++;
y++;   
}

output:
x=2,y=5

x=3,y=6
x=4,y=7


Note that the effect of initialization is seen  only in the first call. In subsequent calls initailization is not performed and variable x and y contain values left by the previous function call.

The next program uses a recursive function to find out the sum of digits of number. The variable sum taken inside function sumd() should be taken as static. 


/* program to find out the sum of digits of a number recursively*/

#include<stdio.h>
int sumd(int num);
main()
{
int num;

printf("Enter a number")
scanf("%d",&num);
printf("sum of digits of %d\n",num,sumd(num));
}
int sumd(int num)
{
static int sum=0;
if(num>0)
{
 sum=sum+(num%10);
sumd(num/10);
}

}

Global static variables


If a local variable is declared as static then it remain alive throughout the program. IN case of global variable , the static specifier is not used to  extend  the life since global variable have already a lifetime equal to the life of program. Here the static specifier is used for information hiding. If an external variable is defined as static , then it can't be used by other files of program. so we can make an external variable private to a file by making it static.



first.c                          second.c                                      third.c
int x=8;                        extern int x;                                 func4()
static int y=10;
main()                          func2()                                        {
{                                  {                                                         .......
........                               .............                                   }
}                                   }                                                func5()
func1()                        func3()                                          {
{                                {                                                          .............
.........                              ...........                                       }
}                                 }


Here the variable y is defined as a static external variable, so it can be used only in the first.c we can't use it in other files by putting ex tern declaration in it.

4.Register



Register storage class can be applied only to local variable. The scope ,life and initial value of register variable are same  as that of automatic variable. The only difference between the two is in the place where they are stored . Automatic variables are stored in memory while register variable are stored in CPU register. Register are small storage units present in the processor. The variable stored in register can be accessed much faster than the variable stored in memory. SO the variables that are frequently used can be assigned register storage class for faster processing . For example the variable used as loop counters may be declared as register variables since they are frequently used.

/*program to understand the use of register variable*/
#include<stdio.h>
main()
{
register int i;
for(i=0;i<20000;i++);
printf("%d\n",i);
}


Register variables don't have memory address so we can't apply address operator(&) to them . There are limited number of register in the processor hence we can declare only few variables as register. if many variable are declared as register and the cpu register are not available the compile will treat them as automatic variables.

The register store class specifier can be applied to formal arguments of a function while the other three storage class specifiers can't be used in this way.

1 comments:

Anonymous
9 June 2013 at 21:51 comment-delete

Nice,good tutorial on storage class i am looking for long time

Reply

Post a Comment

Don't Forget to comment