Storage Classes in C Notes For Beginners
Storage Classes in C Notes For Beginners
Variable name identifies some physical location within the computer where the variable’s
value is stored.
There are basically two kinds of locations in a computer where such a value may be kept
1. Memory
2. CPU registers.
It is the variable’s storage class that determines in which of these two types of locations, the
value is stored.
Variable’s storage class tells us:
(a) Where the variable would be stored.
(b) What will be the initial value of the variable, if initial value is not specifically assigned. (i.e. the
default initial value).
(c) What is the scope of the variable; i.e. in which functions the value of the variable would
be available.
(d) 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
Here the first statement is a declaration, whereas the second is the definition. When we declare
a variable no space is reserved for it, whereas, when we define it space gets reserved for it in
memory.
We had to declare y since it is being used in printf( ) before it’s definition is encountered.
Note: In the following statements the first three are definitions, whereas, the last one is
a declaration.
auto int i ;
static int j ;
register int k ;
extern int l ;
Which to Use When
We can make a rules for usage of different storage classes in different programming
situations with a view to:
(a) economise the memory space consumed by the variables
(b) improve the speed of execution of the 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. Declaring all the variables as extern would amount
to a lot of wastage of memory space because these variables would remain active
throughout the life of the program.
− If you don’t have any of the express needs mentioned above, then use the auto storage
class. In fact, most of the times, we end up using the auto variable. This is because once we
have used the variables in a function and are returning from it, we don’t mind losing them.