Storage Classes
Storage Classes
com
STORAGE CLASSES
INTRODUCTION
m
From C compiler’s point of view, a variable name identifies
co
some physical location within the computer memory,
where the string of bits representing the variable’s value is
n.
stored. There are basically two kinds of locations in a
io
computer where such a value may be kept— Memory and
at
CPU registers. It is the variable’s storage class that
determines in which of these two locations the value is stored.
uc
The use of the storage classes is that,
ed
• The initial value of the variable, if the initial value is not assigned.
ks
www.sakshieducation.com
www.sakshieducation.com
The variable declared under this storage class has the features like,
9 Storage is in memory.
m
9 Default initial value is garbage value, if initial value is not assigned.
co
9 The variable life time is, till the control remains within the block in which the
variable is defined.
n.
main ( )
io
{
at
auto int i, j ;
printf ( "\n%d %d", i, j ) ;
uc
}
ed
1211 221
ks
It’s better to understand the life and scope of a variable clearly from the following
example program.
a
.s
main ( )
{
w
auto int i = 1;
{
w
{
w
{
printf ( "\n%d ", i ) ;
i++;
}
printf ( "%d ", i ) ;
}
2
www.sakshieducation.com
www.sakshieducation.com
}
}
m
This is because, all printf ( ) statements occur within the outermost block (a block is
all statements enclosed within a pair of braces) in which i has been defined. It
co
means the scope of i is local to the block in which it is defined. The moment the
n.
control comes out of the block in which the variable store defined, the variable and
io
its value is irretrievably lost.
at
TOPIC 2 REGISTER STORAGE CLASS
uc
The variable defined under this storage class has the features like,
ed
If a value stored in the CPU can be accessed quickly compared to the value stored in
memory. At times the variable can be used in the program number of times so in
w
this case for accessing the variable its better to declare the variable under the register
w
storage class.
Note:-
3
www.sakshieducation.com
www.sakshieducation.com
Use register storage class for only those variables that are being used very often in a
program. Reason is, there are very few CPU registers at our disposal and many of
them might be busy doing something else. Make careful utilization of the scarce
resources. A typical application of register storage class is loop counters, which get
used a number of times in a program.
m
Example:-
co
main( )
n.
{
register int i ;
io
for ( i = 1 ; i <= 10 ; i++ )
at
printf ( "\n%d", i ) ;
uc
}
• This register storage class is used for the faster access of variables.
ed
9 Storage ----------------------------------memory.
w
4
www.sakshieducation.com
www.sakshieducation.com
Like auto variables, static variables are also local to the block in which they are
declared. The difference between them is that static variables don’t disappear when
the function is no longer active. Their values persist. If the control comes back to the
same function again the static variables have the same values they had last time
around.
m
Program -1 :- Program – 2:-
co
main() main()
{ {
n.
Increment (); Increment ();
io
Increment (); Increment ();
at
} }
Increment (); Increment ();
uc
{ {
ed
I = I + 1; I = I + 1;
ks
} }
Output :- 1 1 Output :- 1 2
a
.s
In the above example, when variable I is auto, each time increment ( ) is called it is
w
re-initialized to one. When the function terminates, I vanishes and its new value of 2
w
is lost. The result: no matter how many times we call increment ( ), I is initialized to
w
1 every time.
www.sakshieducation.com
www.sakshieducation.com
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
m
executed only once, irrespective of how many times the same function is called.
co
Note:-Use static storage class only if you want the value of a variable to persist
between different function calls.
n.
TOPIC 4 EXTERNAL STORAGE CLASS
io
9 Storage: - memory.
at
9 Life: - as long as the program execution comes to an end.
uc
9 Default value: - zero.
9 Scope: - global.
ed
External variables are declared outside all functions, yet are available to all
hi
int i ;
a
.s
main( )
{
w
increment( ) ;
w
decrement( ) ;
decrement( ) ;
}
increment( )
{
i=i+1;
6
www.sakshieducation.com
www.sakshieducation.com
m
i=0
co
On incrementing i = 1
On incrementing i = 2
n.
On decrementing i = 1
On decrementing i = 0
io
The value of i is available to the functions increment ( ) and decrement ( ) since i has
at
been declared outside all functions.
uc
Note: - The local variable that gets preference over the global variable.
ed
Note:- 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
hi
these variables as arguments when making a function call. Declaring all the variables
ks
as extern would amount to a lot of wastage of memory space because these variables
a
7
www.sakshieducation.com