0% found this document useful (0 votes)
33 views3 pages

Strage Classes in C

AKTU PPS NOTES

Uploaded by

krishkumar5362
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views3 pages

Strage Classes in C

AKTU PPS NOTES

Uploaded by

krishkumar5362
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Storage Classes in C:

We declare variables in almost every program. Not every variable has the same features. The
declarations, accessibility in different parts of the program varies from variable to variable depending
on the location the variable is declared. "Storage classes" gives some important information about a
variable like:-
1. Where the variable would be stored.
2. What will be the initial value of the variable, if initial value is not specifically assigned.(i.e.
the default initial value).
3. What is the scope of the variable; i.e. in which functions the value of the variable would be
available.
4. What is the life of the variable; i.e. how long would the variable exist.
There are four storage classes in C:
(a) Automatic storage class
(b) Register storage class
(c) Static storage class
(d) External storage class
Automatic Storage Class:
The features of a variable defined to have an automatic storage class are as under:
Storage − Memory. Default initial
Value − an unpredictable value, which is often called a garbage value.
Scope − Local to the block in which the variable is defined.
Life − Till the control remains within the block in which the variable is defined.
Following program shows how an automatic storage class variable is declared, and the fact that if
the variable is not initialized it contains a garbage value.
main( )
{
auto int i, j ;
printf ( "\n%d %d", i, j ) ;
}
The output of the above program could be...1211 221 where, 1211 and 221 are garbage values of i
and j. When you run this program you may get different values, since garbage values are
unpredictable. So always make it a point that you initialize the automatic variables properly,
otherwise you are likely to get unexpected results. Note that the keyword for this storage class is
auto.
Register Storage Class:
The features of a variable defined to be of register storage class are as under:
Storage - CPU registers.
Default initial value - Garbage value.
Scope - Local to the block in which the variable is defined.
Life - Till the control remains within the block in which the variable is defined.
A value stored in a CPU register can always be accessed faster than the one that is stored in
memory. Therefore, if a variable is used at many places in a program it is better to declare its storage
class as register. A good example of frequently used variables is loop counters. We can name their
storage class as register.
main( )
{
register int i ;
for ( i = 1 ; i <= 10 ; i++ )
printf ( "\n%d", i ) ;
}
Static Storage Class:
The features of a variable defined to have a static storage class are as under:
Storage − Memory.
Default initial value − Zero.
Scope − Local to the block in which the variable is defined.
Life − Value of the variable persists between different function calls.
Compare the two programs and their output given in Figure 6.3 to understand the difference
between the automatic and static storage classes.

if i is static, it is initialized to 1 only once. It is never initialized again. During the first call to increment(
), i is incremented to 2. Because i is static, this value persists. The next time increment( ) is called, i is
not re-initialized to 1; on the contrary its old value 2 is still available. This current value of i (i.e. 2) gets
printed and then i = i + 1 adds 1 to i to get a value of 3. When increment( ) is called the third time, the
current value of i (i.e. 3) gets printed and once again i is incremented. In short, if the storage class is
static then the statement static int i = 1 is executed only once, irrespective of how many times the
same function is called.
External Storage Class:
The features of a variable whose storage class has been defined as external are as follows:
Storage − Memory.
Default initial value − Zero.
Scope − Global.
Life − As long as the program’s execution doesn’t come to an end.
External variables differ from those we have already discussed in that their scope is global, not local.
External variables are declared outside all functions, yet are available to all functions that care to use
them. Here is an example to illustrate this fact.
int i ;
main( )
{ printf ( "\ni = %d", i ) ;
increment( ) ;
increment( ) ;
decrement( ) ;
decrement( ) ; }
increment( )
{i=i+1;
printf ( "\non incrementing i = %d", i ) ;
}
decrement( )
{i=i-1;
printf ( "\non decrementing i = %d", i ) ;
}
The output would be:
i=0
on incrementing i = 1
on incrementing i = 2
on decrementing i = 1
on decrementing i = 0
As is obvious from the above output, the value of i is available to the functions increment( ) and
decrement( ) since i has been declared outside all functions. Look at the following program.
int x = 21 ;
main( )
{
extern int y ;
printf ( "\n%d %d", x, y ) ;
}
int y = 31 ;
Here, x and y both are global variables. Since both of them have been defined outside all the
functions both enjoy external storage class.

You might also like