Register

We can understand all storage classes by differentiating all of those according to their default value, keyword, storage, scope, life. Which are listed in the table below.

Storage Class Keyword Default Value Storage Scope Life
Automatic auto Garbage RAM Limited to block in which it is declared Till execution of block in which it is declared
Register register Garbage CPU registers Limited to block in which it is declared Till execution of block in which it is declared
Static static Zero RAM Limited to block in which it is declared Till the end of the program
External extern Zero RAM Global (everywhere) Till the end of the program

 Automatic

Any variable is by default automatic. This means if we have not written any storage class keyword then it is automatic. Normal variables are automatic, which we are always using in the program.

void main()
{

  int i;  // or we can write auto int i;
  . . .
}
Example program to understand the use of automatic.

Here 1st i is available if the block in which it is declared (main block). But in the 2nd block there is the 2nd i variable, so it is available in that 2nd block so this overwrites the 1st i variable in the 2nd block. But that 1st i variable is accessible outside that 2nd block. The same thing applies for the 3rd block.

#include<stdio.h>>
#include<conio.h>
//1st block
void main()
{
  int i=1;
  clrscr();
  printf("i=%d \n",i);
  //2nd block
  {
    int i=2;
    printf("i=%d \n",i);
  }
  printf("i=%d \n",i);
  //3rd block
  {
    int i=3;
    printf("i=%d \n",i);
  }
  printf("i=%d \n",i);
  getch();
}

Output:

Register

This is exactly the same as the automatic storage class but this variable of register storage class is stored in CPU registers while the variable of automatic storage class is stored in RAM.

This is not 100% guaranteed that the variable of a register is stored only in CPU registers. CPU registers are of limited numbers, so if any CPU register is not free or to be occupied at other work then this variable is stored in RAM (same as an automatic variable).

Normally CPU registers are 16 bits. So we can specify a register storage class for int, char, float, double, pointer types. As this type of variable does not have a memory address (because they are not in RAM), so we can not use &(address) operator with it.

Static

Static variables remain till the end of the whole program. So if it is declared in function and in function, a value is initialized to it then that initialization is done only once. This means if that function is called multiple times then the value is initialized when the function is called 1st time. If we call function 2nd time then that program line of initialization is omitted.

Example program to understand the use of static
#include<stdio.h>>
#include<conio.h>
void call();
void main()
{
  clrscr();
  call();
  call();
  call();
  getch();
}

void call()
{
  static i=1; //this is done only once
  printf("i = %d \n",i);
  i++;
}

Output:

External

This is used when we have declared one variable in one file, and we want to use the same variable in some other files too, then the external storage class variable is used.

So the 1st main file (in which the variable is declared), the declaration of the variable is the same as the normal variable. But if we want to use that variable in some other files then we have to declare that variable with the extern keyword.

The second use of the external storage class variable is in a global variable. The global variable is used in all blocks below its declaration. So in the below picture, x can be used in main block, func1 block, func2 block.

If we try to use that global variable in the block which is made before the declaration of that variable then it throws an error. So in the below picture, if we use x variable in main block, func1 block then it shows error but if we use in func2 block then it works properly.

Example program with error, without use of extern storage class

In this program, x can not be used in main and func1 blocks so it throws an error.

#include<stdio.h>
#include<conio.h>
void func1();
void func2();
void main()
{
    clrscr();
    printf("x=%d \n",x);
    func1();
    func2();
    getch();
}

void func1()
{
    printf("x=%d \n",x);
}

int x=5;
void func2()
{
    printf("x=%d \n",x);
}
Same example program without error, with use of extern storage class
#include<stdio.h>
#include<conio.h>
void func1();
void func2();

void main()
{	
    clrscr();
extern int x;
    printf("x=%d \n",x);
    func1();
    func2();
    getch();
}

void func1()
{	extern int x;
    printf("x=%d \n",x);
}

int x=5;
void func2()
{
    printf("x=%d \n",x);
}

Output:

Lessons learned from this blog:

1) Keywords of storage classes
2) Life, scope, storage, the default value of all storage classes.
3) When to use any storage class and which type to use.
4) Program without using storage class.
5) Program using storage class.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments