Thursday, September 25, 2014

2.9 STORAGE CLASSES

To fully define a variable, one needs to maintain not only its ‘type’ but also its ‘storage class’. A variable’s storage class tells us:
ü  Where the variable would be stored.
ü  What will be initial value of the variable
ü  What is the scope of the variable i.e. in which functions the value of the variable would be available.
ü  What is the life of variable i.e. how long would the variable exist.

There are four storage classes in C:
1.      Automatic storage class
2.      Register storage class
3.      Static storage class
4.      External storage class

Automatic Storage Class
Storage
Memory
Default Initial Value
Garbage
Scope
Local to the block, in which variable is defined
Life
Till the control remains within the block in which the variable is defined

Example:
(i)

#include<stdio.h>
void main()
{
            auto int i = 1;
            {
                        {
                                    {
                                                printf(“%d”,i):
                                    }
                                    printf(“%d”,i);
                        }
                        printf(“%d”,i);
            }
}

OUTPUT
1          1          1

(ii)

#include<stdio.h>
void main()
{
            auto int i, j = 1;
            printf(“%d%d”, i, j);
}

OUTPUT
216321            1

(iii)

#include<stdio.h>
void main()
{
            auto int i = 1;
            {
                        auto int i = 2;
{
                                    auto int i = 3;
                                    printf(“%d”,i):
                        }
                        printf(“%d”,i);
            }
            printf(“%d”,i);
}
 
OUTPUT
3          2          1

 
Register Storage Class
Storage
CPU Registers
Default Initial Value
Garbage
Scope
Local to the block, in which variable is defined
Life
Till the control remains within the block in which the variable is defined

NOTE: A value stored in CPU register can always be accessed faster than the one that is stored in memory.
 
Example:
 
#include<stdio.h>
void main()
{
            register int i;
            for(i = 1; i <= 5; i++)
                 printf(“%d”, i);
}
 
OUTPUT
1          2          3          4          5

 
Static Storage Class
Storage
Memory
Default Initial Value
Zero
Scope
Local to the block, in which variable is defined
Life
Value of the variable persists between different function calls.

Example:
AUTO
#include<stdio.h>
void increment();
void main()
{
            increment();
increment();
increment();
}
void increment()
{
auto int i = 1;
            printf(“%d”, i);
            i++;
}
 
OUTPUT
1          1          1
 
STATIC
#include<stdio.h>
void increment();
void main()
{
            increment();
increment();
increment();
}
void increment()
{
static int i = 1;
            printf(“%d”, i);
            i++;
}
 
OUTPUT
1          2          3
 

 
External Storage Class
Storage
Memory
Default Initial Value
Zero
Scope
Global
Life
As long as the program’s execution does not come to an end.

Example:
#include<stdio.h>
int i;
void increment();
void decrement();
void main()
{
            printf(“%d”, i);
increment();
increment();
decrement();
decrement();
}

void increment()
{
i = i + 1;
            printf(“%d”, i);
}

void decrement()
{
i = i - 1;
            printf(“%d”, i);
}

OUTPUT
0          1          2          1          0

 
WHICH TO USE WHEN
ü  Use static storage class only if you want the value of a variable to persist between different function calls.
ü  Use register storage class for only those variables that are being used very often in a program.
ü  Use extern storage class for only those variables that are being used by almost all the functions in the program. This would avoid unnecessary passing of these variables as arguments when making a function call.
ü  Most of the times, we use auto variables, because often it so happens that once we have used the variables in a function, we don’t mind losing them.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.