STORAGE
CLASSES
In addition to data type, each variable has one more
attribute known as storage class. The proper use of storage classes makes our
program efficient and fast.
The general syntax:
Storage_class
datatype variable_name;
There are four
types of storages classes:
1.
Automatic
2.
External
3.
Static
4.
Register
The keyword auto ,static,register,extern are used for
these above storage classes.
A storage class
decides about these four aspects of a
variable:
1.
Life-time:
Time between the creation and destruction of a variable.
2.
Scope:
Location where the variable is available for use.
3.
Initial
value: Default value taken by an
uninitialized variable.
4.
Place of
storage: Place in memory where the storage is allocated for the variable.
Automatic:
All the variable declared inside a block/function without
any storage class specified are automatic variables.
Lets see the examples:
#include<stdio.h>
Void func(void);
Int main(void);
{
Func();
Func();
Func();
Func();
}
Void func(void)
{
Int x=2,y=9;
Printf(“x=%d,y=%d\n”,x,y);
X++;
Y++;
}
OUTPUT:
X=2,y=9
X=2,y=9;
X=2,y=9;
X=2,y=9;
Explanation:
Here when the function func() is called for first time ,
the variable x and y are created and initialized and when the control returns
to main() , these variables are destroyed. when the function func() is called
for second time again these variables are created and initialized and are destroyed
after execution of the function.
Register:
The scope
,lifetime and initial value of register variables are same as that of automatic
variables. The only difference between the two is in the place where they are
stored. Automatic variables are stored in memory while register variables are
stored in CPU register. Registers are small storage units present in the
processor. The variable stored in registers can be accessed much faster than
the variables stored in memory so the variables that are frequently used can be
assigned register storage class for
faster processing .
Lets see the example:
#include<stdio.h>
Int main(void)
{
Register int I;
For(i=0;i<2000;i++)
Printf(“%d\n”,i);
Return 0;
}
NOTE: Register variables don’t have memory addresses so
we can’t apply address operator (&) to them.
External:
The variables that have to be used by many functions and
in different files can be declared as external variables. The initial value of
an uninitialized external variable is zero.
Before studying external variables. Lets us first
understand the difference between their definition and declaration. The
declaration of an external variable declares the type and name of the variable,
while the definition reserves storages for the variable as well as behaves as a
declaration.
Note: The keyword extern is specified in declaration but
not in definition. For example the definition of an external variable salary
will be written as :float salary;
Its declaration will be written as: extern
float salary;
Definition of an external variable:
a)
Definition creates the variable , so memory is
allocated at the time of definition.
b)
There can be only one definition
c)
The variable can be initialized with the definition
and initializer should be constent
d)
The definition can be written only outside of
function.
Declaration of an external variable:
a) Declaration does not create a variable, it only refers
to a variable that has already been created somewhere, so memory is not
allocated at the time of declaration.
b) The variable can not be initialized at the time of
declaration
c) The keyword extern is always specified in the
declaration.
Static:
There are two
types of static variables:-
1.
Local
static variables.
2.
Global
static variables.
1.Local static variables
The scope of a
local variable is same as that of an automatic variable l.e it can be used only
inside the function or block in which it is defined. The lifetime of static
variable is more than that of automatic
variable. A static variable is created at the compilation time and it remain
alive the end of a program.
A static variable
can be initialized only by constant or constant expression if a static variable
is not explicitly initialzed then by default it takes initial value zero(‘0’).
Program to
understand the use of local static variable:
#include<stdio.h>
void func(void);
int main(void)
{
func();
func();
func();
return 0;
}
Void func(void)
{
Static int x=2,y=5;
Printf(x=%d,y= %d\n”,x,y);
X++;
Y++;
}
Output: x=2,y=5
X=3,y=6
X=4,y=7
2.Global static variable:
If a
local variable is declared as static then it remains alive throughout the
program. In case of global variable, the static specified is not used to extend
the lifetime since global variable already have a lifetime equal to the life of
program.
If an external variable is
defined as static then it can’t be used by other files of the program so we can
make an external variable private to a file by making it static.