Translate

Labels

Monday 14 January 2013

STORAGE CLASS

Storage class is another attribute that is associated with the variable.C provides four storage classes:

  •                    auto,
  •                    static,
  •                    register,
  •                    extern.

A variable's storage class is used to determine its scope and  lifetime auto variables are actually local variables.They are created when the function in which they are declared is entered, and they are destroyed when the function is exited.The variable declared within a function are local to that function.Their scope and lifetime are limited within that function,which means, all the local variables are destroyed when the function execution is over.We cannot access the values of the local variable outside the function.., i.e..,the scope of the local variables is within the function in which they have been declared.consider the following program:
#include<stdio.h>
main()
{
add();
add();
}
add()
{
int i=0;
i=i+1;
printf("%d\n",i);
}
The output of the above program will be 
1
1
      The variable i is a local variable in the add function.Each time the add function is called, the variable i gets recreated and initialized to 0 and hence the result will be always 1.If the variable has been declared as a static variable, its value will be retained even after the function execution is over.change the add function as follows:
add()
{
static int i=0;
i=i+1;
printf("d\n",i);
}
       The variable i has been declared as a static variable.It is local to the add function.Its scope is still within the add function and its value cannot be accessed by any another function.The static variables are created only once during the first call of the function.The main advantage of static variable is that their values are retained even after execution of the function.So,each time, the function add is called, the value of the variable i gets incremented and output will be 
1
2
      The above program is rewritten by declaring i as a global variable.Global variable are declared before the main function.They can be accessed and modified by all the functions in the program.
#include<stdio.h>
int i=0;
main()
{
add();
add();
}
add()
{
i=i+1;
printf("%d\n",i);
}
The output of the above program will be:
1
2
      In the above program, i is a global variable and it can be accessed modified in all functions.The static variables retain the characteristics of local variables.The life time of the static variable as well as the global variable ends only when the entire program execution is over.The scope of the static variable is to the function in which it has been declared.The scope of the global variable is to all the functions in the program.Except these differences, the global variables declared outside any function are static by default.But static variable is not a global variable.
       The register variable behave like auto variables if a variable is declared with register storage class,its value is placed in one of the computer's high-speed hardware registers.If the compiler does not find sufficient variable are used to speed up operations, by reducing memory access time. 
     Global  variable are accessed in the function of a program stored in one file. If the global variable have to be accessed by the function in a file other than the one in which they are declared, the extern storage class can be used.For example, if we define global integer variable count in one file, and refer to it in a second file the second file must contain the declaration extern int count; prior to the variable's use in that file.The extern variables have global scope and the lifetime is throughout the execution of the program

No comments: