Thursday, 20 June 2013

STORAGE CLASS in C language

STORAGE CLASS
There are 2 different ways to characterize variables:
By datatype
By storage class
DATATYPES
It refers to the type of information represented by a variable eg., integer number, floating point number, character etc.
STORAGE CLASS
A 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.
TYPES:
(a) Automatic storage class (keyword is auto)
(b) Register storage class (keyword is register)
c) Static storage class (keyword is static)
(d) External storage class (keyword is external)
NOTE Keyword that specifies a particular storage class must be placed at the beginning of the variable declaration.
AUTOMATIC STORAGE CLASS
Storage − Memory.
Default initial value − an unpredictable value, which is 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.
Keyword auto is used to declare the automatic variable.
Automatic variables are always declared within the function and are local to the function in which they are declared. Their scope is confined to that function.
Automatic variables defined in different functions will therefore be independent of one another, even though they may have the same name.
Memory is allocated automatically upon entry to a block and freed automatically upon exit from the block.
The values of automatic variable will be reassigned each time the function is reentered.
An automatic variable does not retain its value once control is transferred out of its defining function.
Therefore, any value assigned to an automatic variable within the function will lost once the function is exited.
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.
Scope and life of an automatic variable is illustrated in the following program.
main( )
{
 auto int i = 1 ;
 {
  auto int i = 2 ;
  {
  auto int i = 3 ;
  printf ( "\n%d ", i ) ;
  }
  printf ( "%d ", i ) ;
 }
 printf ( "%d", i ) ;
}
 The output of the above program would be: 3 2 1
Note that the Compiler treats the three i’s as totally different variables, since they are defined in different blocks.
Once the control comes out of the innermost block the variable i with value 3 is lost, and hence the i in the second printf( ) refers to i with value 2.
Similarly, when the control comes out of the next innermost block, the third printf( ) refers to the i with value 1.
EXTERNAL STORAGE CLASS
The features of External variable
Storage − Memory.
Default initial value − Zero.
Scope − Global.
Life − As long as the program’s execution doesn’t come to an end.
External variables are declared outside all functions
The scope of the external variables extends from the point of its definition through the remainder of the program.
The external variable can be assigned a value within one function, and this value can be used within another functions

NOTE:
An external variable declaration does not reserve storage space. Hence, a definition is essential to create external variable.
External Variable Definition:
It is also looks like a ordinary variable declaration.
It must appear outside of, and usually before the functions that access the external variables.
When an external variable is defined, memory is allocated for it.
The keyword ‘extern’ is not required in an external variable definition.
Syntax:
Datatype variable=value;
An external variable must be defined only once.
External Variable Declaration:
Syntax:
extern datatype var1,var2…var-n;
§ An external variable declaration cannot include the assignment of initial values.
§ The memory is allocated for the external variable when the program starts, and the memory is only released when the program ends.
Example:
int i ;
main( )
{
 printf ( "\ni = %d", i ) ;
 increment( ) ;
 increment( ) ;
 decrement( ) ;
 decrement( ) ;
}
increment( )
{
 i = i + 1 ;
printf ( "\n on incrementing i = %d", i
}
decrement( )
{
 i = i - 1 ;
 printf ( "\n on 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



STATIC STORAGE CLASS
The features of static storage class:
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.
Example for auto variable:
 main( )
 {
  increment( ) ;
  increment( ) ;
  increment( ) ;
 }
 increment( )
 {
  auto int i = 1 ;
  printf ( "%d\n", i ) ;
  i = i + 1 ;
 }
The output of the above programs would be:
1
1
1

main( )
{
  increment( ) ;
  increment( ) ;
  increment( ) ;
}
 increment( )
{
  static int i = 1 ;
  printf ( "%d\n", i ) ;
  i = i + 1 ;
}
The output of the above programs would be:
1
2
3

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.

REGISTER STORAGE CLASS
Features
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.
main( )
{
 register int i ;

 for ( i = 1 ; i <= 10 ; i++ )
  printf ( "\n%d", i ) ;
}


No comments:

Post a Comment