STROAGE CLASS


Storage Class :

'Storage' refers to the scope of a variable and memory allocated by compiler to store that variable. Scope of a variable is the boundary within which a varible can be used. Storage class defines the the scope and lifetime of a variable.
From the point view of C compiler, a variable name identifies physical location from a computer where varaible is stored. There are two memory locations in a computer system where variables are stored as : Memory and CPU Registers.
Functions of storage class :
To detemine the location of a variable where it is stored ?
Set initial value of a variable or if not specified then setting it to default value.
Defining scope of a variable.
To determine the life of a variable.

Types of Storage Classes :

Storage classes are categorised in 4 (four) types as,

Every one is breifly explained below










  • Keyword : auto
  • Storage Location : Main memory
  • Initial Value : Garbage Value
  • Life : Control remains in a block where it is defined.
  • Scope : Local to the block in which variable is declared.
Syntax :

 auto [data_type] [variable_name];
 
Example :

 auto int a;
 

Program :

/*  Program to demonstrate automatic storage class.

Creation Date : 30 MAR 2012 3:05:11 PM

Author :HARISH */

#include <stdio.h>
#include <conio.h>
void main()
{
 auto int i=10;
 clrscr();
 {
  auto int i=20;
  printf("\n\t %d",i);
 }
 printf("\n\n\t %d",i);
 getch();
}

Output :


 20

 10_

















  • Keyword : register
  • Storage Location : CPU Register
  • Initial Value : Garbage
  • Life : Local to the block in which variable is declared.
  • Scope : Local to the block.
Syntax :

 register [data_type] [variable_name];
 
Example :

 register int a;
 
When the calculations are done in CPU, then the value of variables are transferred from main memory to CPU. Calculations are done and the final result is sent back to main memory. This leads to slowing down of processes.
Register variables occur in CPU and value of that register variable is stored in a register within that CPU. Thus, it increases the resultant speed of operations. There is no waste of time, getting variables from memory and sending it to back again.
It is not applicable for arrays, structures or pointers.
It cannot not used with static or external storage class.
Unary and address of (&) cannot be used with these variables as explicitly or implicitly.

Program :

/*  Program to demonstrate register storage class.

Creation Date : 30 MAR 2010 3:10:55 PM

Author : HARISH */

#include <stdio.h>
#include <conio.h>

void main()
{
 register int i=10;
 clrscr();
 {
  register int i=20;
  printf("\n\t %d",i);
 }
 printf("\n\n\t %d",i);
 getch();
}

Output :


 20

 10_




















  • Keyword : static
  • Storage Location : Main memory
  • Initial Value : Zero and can be initialize once only.
  • Life : depends on function calls and the whole application or program.
  • Scope : Local to the block.
Syntax :

 static [data_type] [variable_name];
 
Example :

 static int a;
 
There are two types of static variables as :

a) Local Static Variable
b) Global Static Variable
Static storage class can be used only if we want the value of a variable to persist between different function calls.

Program :

/*  Program to demonstrate static storage class.

Creation Date : 30 MAR 2012 3:14:22 PM

Author : HARISH */

#include <stdio.h>
#include <conio.h>

void main()
{
 int i;
 void incre(void);
 clrscr();
 for (i=0; i<3; i++)
 incre();
 getch();
}

void incre(void)
{
 int avar=1;
 static int svar=1;
 avar++;
 svar++;
 printf("\n\n Automatic variable value : %d",avar);
 printf("\t Static variable value : %d",svar);
}

Output :


Automatic variable value : 2 Static variable value : 2

Automatic variable value : 2 Static variable value : 3

Automatic variable value : 2 Static variable value : 4_
















  • Keyword : extern
  • Storage Location : Main memory
  • Initial Value : Zero
  • Life : Until the program ends.
  • Scope : Global to the program.
Syntax :

 extern [data_type] [variable_name];
 
Example :

 extern int a;
 
The variable access time is very fast as compared to other storage classes. But few registers are available for user programs.
The variables of this class can be referred to as 'global or external variables.' They are declared outside the functions and can be invoked at anywhere in a program.

Program :

/*  Program to demonstrate external storage class.

Creation Date : 30 MAR 2010 3:15:04 PM

Author : HARISH*/

#include <stdio.h>
#include <conio.h>

extern int i=10;
void main()
{
 int i=20;
 void show(void);
 clrscr();
 printf("\n\t %d",i);
 show();
 getch();
}
void show(void)
{
 printf("\n\n\t %d",i);
}

Output :


 20

 10_












No comments: