Storage Classes IN C


Storage Classes
'Storage' refers to the scope of a variable and memory allocated by compiler to store that variable. Scope of a variable is the boundary within which a variable can be used. Storage class defines the scope and lifetime of a variable.
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.
Types of Storage Classes:
Storage classes are categorized in 4 (four) types as,
·      Static Storage Class

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.

o    Keyword : static
o    Storage Location : Main memory
o    Initial Value: Zero and can be initialize once only.
o    Life : depends on function calls and the whole application or program.
o    Scope: Local to the block.

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 memorize 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 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.

Register Storage class

o    Keyword : register
o    Storage Location : CPU Register
o    Initial Value : Garbage
o    Life : Local to the block in which variable is declared.
o    Scope : Local to the block.
#include <stdio.h>
#include <conio.h>
extern int i=10;
void main()
{
               int i=20;
               void show(void);
               clrscr();
               printf("\n\t %d",i);
               show();
               getch();
}
void show(void)
{
               printf("\n\n\t %d",i);
}

OUTPUT:

20
10

Register Storage Class:

o    Keyword : register
o    Storage Location : CPU Register
o    Initial Value : Garbage
o    Life : Local to the block in which variable is declared.
o    Scope : Local to the block.
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