3/18/2015



Storage classes in C Programming


A storage class defines the scope(visibility) and liftime of variables and/or functions within a C program. These specifiers precede the type that they modify. There are following storage classes that can be used in c program.
  • auto
  • register
  • static
  • extern
The auto Storage Class :
        The auto storage class is default storage class of all local variables.
    {
           int sum;
           auto int sum1;
    }
      The example above defines two variables with the same storage class, auto can only be used within functions, i.e., local variables.

The register Storage Class :
        The register storage class is used to define local variables that should be stored in a register instead of RAM. This means that variable has a maximum size equal to the register size(usually one word).
    {
           register int sum;
    }
      The register should only be used for variables that requires quick access such as counters. Is should also be noted that defining 'register' does not mean that the variable will be stored in a register. It means that it MIGHT be stored in register depending on hardware and implementation restrictions.

The static Storage Class :
        The static storage class iinstruct compiler to keep a local variables in existance during the lifetime of the programm istead of creating and destroying it each time it comes into and goes out of scope. Therefore making local variables static allows them to maintain their values between function calls.
The static modifier may also applied to global variables. When this is done, it causes that variable scope to be resricted to the file in which it is declared.

#include
/* function declaration */
void func();
static int count = 5; /* global variable */

main()
{
while(count--)
    {
           func();
    }
}
/* function declaration */
void func()
{
static int i = 5; /* local static variable */
i++;

printf(" i is %d and count is %d\n", i, count);

      When the above code is compiled and executed, it produces the following result:
i is 6 and count is 4
i is 7 and count is 3
i is 8 and count is 2
i is 9 and count is 1
i is 10 and count is 0

The extern Storage Class :
        The extern storage class is used to give reference of global variables that is visible to ALL the program files. When u use 'extern', the variable cannot be initialized as all it does is point the variable name at a storage location that has been previously defined.
When u have multiple files and you define a global variable or function, which will be used in other files also, then extern will be used in another file to give reference of defined variable or function in another file. Example is as below.
    {
           register int sum;
    }
      The register should only be used for variables that requires quick access such as counters. Is should also be noted that defining 'register' does not mean that the variable will be stored in a register. It means that it MIGHT be stored in register depending on hardware and implementation restrictions.

No comments:

Post a Comment