The different locations in the computer where we can store data and their accessibility, initial values etc. vary based on the way they are declared. These different ways are termed as different storage classes. In C, we have four storage classes, namel
- Automatic
- Register
- Static
- External or Global
Let us see these storage classes one by one
- Automatic storage class
In this case data is stored in memory
The initial value of such a variable is
garbage.
The scope of the variable is local i.e. limited
to the function in which it is defined.
The life of such variables is till the control
remains in the particular function where it is
defined.
For e.g.:
int i; or auto int i;
In all our programs till now we have been
using the automatic storage class for our
variables.
- Register storage class
In this case data is stored in CPU register
The initial value of such a variable is
garbage.
The scope of the variable is local i.e. limited
to the function in which it is defined.
The life of such variables is till the control
remains in the particular function where it is
defined.
For e.g.:
register int i;
In this case the data is stored in a small
memory inside the processor called as its
registers.
The advantage of such storage class is that
since the data is in the processor itself, its
access and operations on such data is faster.
There is a limitation on the size of the data
that can be declared to be register storage
class. The data should be such that it doesn’t
require more than 4 bytes. Hence double and
long double data types cannot be declared as
register.
Also there is a limitation on the maximum
number of variables in a function that can be
of register class. The limitation is that a
maximum of 3 register class variable can be
declared in a function.
- Static storage class
In this case data is stored in memory
The initial value of such a variable is zero.
The scope of the variable is local i.e. limited
to the function in which it is defined.
The life of such variables is till the program
is alive.
For e.g. :
static int i;
If a variable is declared static, its value
remains unchanged even if the function
execution is completed.