storage class in C Language

PREVIOUS                                                                                                                 NEXT



storage class in C Language

Basically there is two types of memory location where the computer stores the variable. 
1. Memory
2.CPU Register
Types of storage class

    1. Automatic storage class
    2. Register storage class
    3. Static storage class
    4. External storage class
Before we proceed we must know the features of the storage class
Features of the storage class of a variable
    1. Where the variable is stored
    2. What is the initial value of a variable i.e default value if any specific value is not assigned at the time of variable declaration.
    3. What is the scope of a variable i.e where the variable can be accessible.
    4. What is the life of a variable.

Automatic Storage Class

Let us explain with the below example
main()
{
     int a;
     printf("%d,a");
}
In this program when we declare int a and when the compiler execute the statement and since no storage class type is specified the compiler treats it as an automatic storage class. The only automatic storage class is optional i.e we may  explicitly  mention it  with the variable definition i.e auto int a; or may not  i.e int a; if not explicitly mentioned with the variable definition the compiler implicitly treats it as the variable is declared as automatic storage class type but the rest register storage class, static storage class and external storage class has to explicitly mentioned with the variable definition.
When the compiler execute the next statement the out put is unpredictable.Each time you execute you will get different results.
The above example can be written as
main()
{
     auto int a;
     printf("%d,a");
}
Here the out put is garbage.
main()
{
     auto int a = 2;
     printf("%d,a");
}
Here the out put is 2. 
Because here we have explicitly assign a value to the auto variable.

Let us see another example

main()
{
    auto int a=2;
    {
        auto int b=3;
        printf("%d %d",a,b);
     }
     printf("%d",a);
}
Here the out put is 2 3 2.Here 

Let us see another example
main()
{
    auto int a=2;
    {
        auto int b=3;
        printf("%d %d",a,b);
     }
     printf("%d %d",a,b); // compiler error
}

Here when we compile the program an error message will come. The integer variable a is declared in outer most block, so it is accessible any where in this block including in the inner block also. But the integer variable b is declared in the inner block i.e within the outer most block, so the variable b is accessible  only within the inner block it cannot be accessible in the outer block. So when we tray to access the variable in outer block defined within the inner block i.e here variable b the compiler could not fine the definition part of the variable so it shows an error.

main()
{
    auto int a=2;
    {
        auto int a=3;
        printf("%d ",a);
     }
     printf("%d",a);
}
Here the out put is 32.
Here the compiler treats both the outer and the inner block variable a as two different variable. so when the compiler access the inner block variable a =3 is accessed. When the control comes to the outer block the variable a=2 is accessed.

Note:

          When a variable is defined without any storage class specifier then the compiler by default treats it as an automatic storage class type variable. 

The Features Of  The Automatic Storage Class

    1. Storage  Memory  - In Temporary Memory
    2. Scope Of The Variable    - within The Block in which the variable is defined
    3. Default Initial value Of The Variable  - Garbage i.e not predictable
    4. Life Of The Variable  - Till The Control Remains within the block where the variable is defined

 Register Storage Class

When a variable is declared as a register storage class type then the variable is stored in the computer CPU register memory. The register memory are accessed faster than all the three storage class type memory. When a variable is used frequently in the program then we should declare the variable as register type variable.

Let us see an example
main()
{
    register int a;
    for(a=1;a<10;a++)
            ;
}
In this program the variable is accessed frequently, increased its value then store then again access. for this  we declare the variable as register storage class type.

Note:

    1. We cannot give any guaranty that a variable is declared as register storage class type is stored in CPU register.
    2. When a register type variable is not stored in CPU register then it is stored in memory.
    3. When the compiler does not treats the register storage class type variable as a register storage class type then it treats it as an auto storage class type by                 default.
    4. The compiler decides that the variable will store in CPU memory  or in memory.
    5. If a double or float type variable is declared as register storage class type variable then the compiler does not show any error. 
        
The Features Of  The Register Storage Class

    1. Storage  Memory  - In CPU Register Memory
    2. Scope Of The Variable    - within The Block in which the variable is defined
    3. Default Initial value Of The Variable  - Garbage i.e not predictable
    4. Life Of The Variable  - Till The Control Remains within the block where the variable is defined

 Static Storage Class

Let us see an example. In this example we are using functions to explain this example. We want to print the value of the variable a in the function like 2 3 .

Let us see first with this example
print();
main()
{
     int a;
     print();
     for(a=2;a>0;a--)
       {
          print();
       }
}

print()
{
   int a=2;
   printf("%d",a);
   a++;
}

Here the out put is 2 2 2

When the program executes and when the control execute the for loop it first assign (auto storage class type - the default declaration of  the variable) integer value 2 to the variable a and the test the condition, here 2>0 is true so the control execute the body of the for loop  and execute the function call. Then the control execute the body of the print function. First it creates a integer type variable, (auto storage class type - the default declaration of  the variable, allocate the 2 bytes of memory for this variable in the data segment and name the variable as a and assign the integer constant value 2 to the variable.) Then the control execute the printf function, and print the value of the variable 2 in the screen after that the control execute the a++ statement. And the value of the variable is incremented by 1 i.e now the value of the variable is 3. And after that the control execute the end of the function of print function.

Now since the integer variable a declared in the print function as an auto storage class type variable so the life of this variable is within the block of the function. So when the control execute the end of the function the variable a is dead and the value of the variable is lost.

After execute the end of the print function the control execute the a-- of the for loop in the main function. And the value of the variable a is 1, and after that the control execute the conditional statement a>0, here 1>0 is true and the control execute the print function call. And the same happened as we discussed above. And prints again the value of the variable 2 in the screen

And after execute the print function the control execute a-- of for loop in main function and the value of the variable a is 0, and after that the control execute the conditional statement a>0, here 0>0 which is false and the for loop is terminated and then the control execute the end of the main function.

So the out put is 2 2 not 2 3.

Let us declare the variable a declared in the print function as static storage class type.

Let us see the same example in a little twist
print();
main()
{
     int a;
     print();
     for(a=2;a>0;a--)
       {
          print();
       }
}

print()
{
   static int a=2;
   printf("%d",a);
   a++;
}

When the program executes and when the control execute the for loop it first assign (auto storage class type - the default declaration of  the variable) integer value 2 to the variable a and the test the condition, here 2>0 is true so the control execute the body of the for loop  and execute the function call. Then the control execute the body of the print function. First it creates a static storage class type integer variable and allocate 2 bytes of memory for this variable in the data segment and name the variable as a and assign the integer constant value 2 to the variable.Then the control execute the printf function and print the value of the variable 2 in the screen after that the control execute the a++ statement. And the value of the variable is incremented by 1 i.e now the value of the variable is 3. And after that the control execute the end of the function of print function.

Now since the integer variable a declared in the print function as a static storage class type variable so the life of this variable is persist till the end of the program. And it initialize the variable at the time of declaration  even if the print function executes once more the variable inside the print function never initialized again it has the value 3. So when the control execute the end of the function the variable declared as static type never dead till the end of the program and it retains its value.

After execute the end of the print function the control execute the a-- of the for loop in the main function. And the value of the variable a is 1, and after that the control execute the conditional statement a>0, here 1>0 is true and the control execute the print function call. And the here the initialize statement is not executed and the printf function prints the value of the variable on the screen i.e 3 on the screen after that the control execute the a++ statement. And the value of the variable is incremented by 1 i.e now the value of the variable is 4. And after that the control execute the end of the function of print function.

And after execute the print function the control execute a-- of for loop in main function and the value of the variable a is 0, and after that the control execute the conditional statement a>0, here 0>0 which is false and the for loop is terminated and then the control execute the end of the main function.

So the out put is 2 3.

The Features Of  The Static Storage Class

    1. Storage  Memory  - In Memory
    2. Scope Of The Variable    - within The Block in which the variable is defined
    3. Default Initial value Of The Variable  - Zero
    4. Life Of The Variable  - Till The end of the program.

 External Storage Class

Let us see an example. In this example we are using functions to explain this example.

Let us see first with this example

int b = 2;
main()
{
     int a = 3;
     printf("%d %d",a,b);
 }

When control executes the printf function in the main function , when the control access the variable a it first looks for the local integer type variable declared with the name a and since there is a local variable is defined in the main function as a so the control can access the value of the local variable, but when the control access the variable b it first looks for the local integer type variable declared with the name b and since there is no such type of local variable is defined in the local block the control looks for the global variable defined above the main function and we declared the global variable above the main function so it can access the global variable.

When we run the program it first creates an external storage class type variable, allocates 2 bytes of memory for this, assigns integer constant 2 and name it as b.Then it creates a local auto integer type variable, allocates 2 bytes of memory for this, assigns integer constant 3 and name it as a. Now the control executes printf function, and prints  3 2 on the screen.

Let us see another example
main()
{
     int a = 3;
     printf("%d %d",a,b);
}
int b = 2;

Here the compiler error comes

When control executes the printf function in the main function , when the control access the variable a it first looks for the local integer type variable declared with the name a and since there is a local variable is defined in the main function as a so the control can access the value of the local variable, but when the control access the variable b it first looks for the local integer type variable declared with the name b and since there is no such type of local variable is defined in the local block the control looks for the global variable defined above the main function but we declared the global variable below the main function so it cannot access the global variable. so the compiler gives an error message. 
 
Here we change the position of the declaration statement for the external storage class variable.We declared it below the main function block. But in the previous one we declared it above the main function block. We know that in c language the every statement is executed from top to bottom of the program.

So in the first example the global variable ( The external storage class type variable is called global variable ) is executed first so before the execution of the main function so this variable is accessed by the printf function in the main function.
But in the second example the global variable is defined below the main function and it is not accessed by the printf function in the main function.

Let us see how to avoid this error
main()
{
     int a = 3;
     extern int b;
     printf("%d %d",a,b);
}
int b = 2;

When compiler executes the extern int b; statement it confirms that integer type variable  b is declared some where else globally in the program outside the block. when the control executes printf function in the main function , when the control access the variable a it first looks for the local integer type variable declared with the name a and since there is a local variable is defined in the main function as a so the control can access the value of the local variable, now when the control access the variable b it first looks for the local integer type variable declared with the name b and since there is no such type of local variable is defined in the local block and then control looks for the global variable defined some where else in the program  and access that variable.

The Features Of  The External Storage Class

    1. Storage  Memory  - In Memory
    2. Scope Of The Variable    - Global
    3. Default Initial value Of The Variable  - Zero
    4. Life Of The Variable  - Till The end of the program.

PREIVIOUS                                                                                                               NEXT

No comments:

Post a Comment