Storage class: – It is a class which is use to improve the efficiency of program. There are four types of storage class.

  1. Automatic (auto)
  2. Register (register)
  3. External (extern)
  4. Static (static)

There are four parameters have been use for all four types of storage class.

  1. Lifetime
  2. Scope
  3. Initial value
  4. Memory

Automatic storage class: – All non-static local variables which are either inside or any block are automatic variable. If before non-static local variable, we do not provide any storage class name then by default that variable will be of automatic variable.

Ex: –

void main()
{
                int a;
                auto int b;
                //Both a, b automatic variable
}
Lifetime Same within function only
Scope
Initial value Garbage
Memory Stack memory

Register storage class: – All automatic variables are register variable. But the difference between them is automatic variable will be stored inside memory but register variable will be stored inside CPU register. So, register variable one comparatively faster than automatic variable but inside CPU there the limited number of register so we cannot make more local variable as register variables.

If few local variable need to be use frequently inside local variable then for those local variables we can make as register variable.

Ex: –

void main()
{
                register int a,b;
                register char ch;
                //'a' register variable 'b' automatic variable
}
Lifetime Same within function only
Scope
Initial value Garbage
Memory CPU register

Note: –

  1. We cannot get the address of register variable.
  2. Register variable only applied to integer or character variables.

External storage class: – The external variables will be accessed inside all the function as well as inside different file. External variable is nothing but global variable.

Ex: –

     TEST.H
int i=9;  //Global Variable
     DEMO.CPP
#include<iostream.h>
#include<conio.h>
#include"TEST.H"
extern int i;         //External Variable
void main()
{
                clrscr();
                cout<<"External Variable : "<<i<<endl;
                getch();
}
Lifetime Within file
Scope Within file as well as different file
Initial value Zero
Memory Uninitialized data area

Static storage class: –

Global static variable: –

Ex: –

                static int i;
Lifetime Inside the program or file
Scope
Initial value Zero
Memory Uninitialized data area

Local static variable: –

Ex: –

void main()
{
                static int i;
}
Lifetime Inside the program or file
Scope Only inside that function
Initial value Zero
Memory Uninitialized data area
Advertisements