Storage Classes in C Language


Storage Classes
The storage class determines the part of member storagegrey_loader
  is allocated for an object and how long the storage allocation continues to exit.
Storage class tells us:
1) Where the variable is stored.
2) Initial value of the variable.
3) Scope of the variable. Scope specifies the part of the program which a variable is accessed.
4) Life of the variable.

A storage class defines the scope (visibility) and life time of variables and/or functions within a C Program.
           
Every variable and function in C has two attributes: type and storage class. The four storage calsses in c are automatic, static, external and register with corresponding keywords.
1)      Auto          2)  Static                    3) extern                     4) register

Automatic variables:
                        These are the variables that are declared without using any storage class specification or by susing a storage calss specification ‘auto’. The scope of such variables  are limited to the block of the function in which they are declared. Once the exexcution of the block, or function is complete, these variables are disposed off. auto is the default storage classs for local variables.

Declaration synatax:
      Auto type-variables list;
                  Or
      Type variable list;
Example:
            auto int a,b,c;
            auto float x,y;
            int x,y,z;
            float  a,b,c;

Features of Automatic Variables:
·         Storage Space : main memory,
·         Default Initial value: garbage,
·         Scope: : Limited to a block or function in which they are declarted,
·         Life : it remains till the completion of execution of the block or function where they are defined.

Example 1:
main()
{
auto int i=10;
printf(“%d”, i);
}
Output:
10
Example 2:
main()
{
auto int i;
printf(“%d”, i);
}
Output:
1002
In example 1, i value is initialized to 10.So, output is 10.
In example 2, i value is not initialized. So, compiler reads i value is a garbage value.


Static
Static is the default storage class for global variables. The two variables below (count and road) both have a static storage class.
    static int Count;
        int Road;
        {
            printf("%d\n", Road);
        }
Static variables can be 'seen' within all functions in this source file. At link time, the static variables defined here will not be seen by the object modules that are brought in.
Static can also be defined within a function. If this is done the variable is initialized at run time but is not reinitialized when the function is called. This inside a function static variable retains its value during various calls.
Example:
void func(void);
  
   static count=10; /* Global variable - static is the default */
      main()
   {
     while (count--)
     {
         func();
     }
    }
  
   void func( void )
   {
     static i = 5;
     i++;
     printf("i is %d and count is %d\n", i, count);
   }
    
 This will produce following result
 
   i is 6 and count is 9
   i is 7 and count is 8
   i is 8 and count is 7
   i is 9 and count is 6
   i is 10 and count is 5
   i is 11 and count is 4
   i is 12 and count is 3
   i is 13 and count is 2
   i is 14 and count is 1
   i is 15 and count is 0

NOTE: Here keyword void means function does not return anything and it does not take any parameter. We can memoriese void as nothing. Static variables are initialized to 0 automatically.

Example :
main()
{
add();
add();
}
add()
{
static int i=10;
printf(“\n%d”,i);
i+=1;
}
Output:
10
11

Extern
Extern is used to give a reference of a global variable that is visible to ALL the program files. When we 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 we have multiple files and we 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. Just for understanding extern is used to declare a global variable or function in another file.
File 1: main.c                   
   int count=5;
   main()
   {
     write_extern();
   }
File 2: write.c
   void write_extern(void);
   extern int count;
   void write_extern(void)
   {
     printf("count is %i\n", count);
   }
Here extern keyword is being used to declare count in another file.
Now compile these two files as follows

   gcc main.c write.c -o write
This fill produce write program which can be executed to produce result.
Count in 'main.c' will have a value of 5. If main.c changes the value of count - write.c will see the new value
Register
Register is used to define local variables that should be stored in a register instead of RAM. This means that the variable has a maximum size equal to the register size (usually one word) and can’t have the unary '&' operator applied to it (as it does not have a memory location).
               {
            register int  Miles;
               }
Register should only be used for variables that require quick access - such as counters. It should also be noted that defining 'register' goes not mean that the variable will be stored in a register. It means that it MIGHT be stored in a register - depending on hardware and implementation restrictions.

Followers