Storage Classes in C

By | July 30, 2019

When we declare a variable in C with its declaration, we can tell some of its properties such as variable name, variable data type, and variable size in the memory. Apart from these three properties variable also possess some other properties such as variable Default Value, Variable Storage, Variable Scope and variable life. With simile variable declaration we cannot tell all the preparties of a variable but with the help of Storage class, we can do.

What is a Storage Class?

Do not confuse it with the Class Data Structure, Storage Class means Storage classification of a variable. It is a concept in C which can reveal all the unknown properties of a variable such as variable Default Value, Variable Storage, Variable Scope and variable life.

Variable Properties Descriptions
Default value For e.g.  int x;

What would be the default value of the variable x if we have not defined it during the variable declaration? Would it be a garbage value or something else?

Variable Storage The variable could be stored in two memory locations it could be CPU main memory (RAM) or CPU registers.
Variable Scope The scope of the variable defines the visibility of a variable in the whole program.
Variable life What would be the life cycle of the variable means the life period of the variable from the creation of the variable to the destruction of the variable?

Storage Classes in C

With the help of Storage Class, we can classify all these above properties of a variable at the time of its declaration.

Storage Class Specifiers

There are four types of Storage Class in C which are known as Storage Class Specifiers.

  • Automatic (auto)
  • Register (register)
  • External (extern)
  • Static (static)

Syntax of Class Storage Specifier in C

<Class Storage Specifier> <data type> <variable name>;

Example:

auto int x;

Note: Storage Class specifier are optional if we do not define them, by default our variable will considered as auto Storage Class Specifier.

Storage Class Specifier Keyword Storage Default Value Scope Life
Automatic auto RAM Garbage Limited to the block Limited to the block execution
Register register Register Garbage Limited to the block Limited to the block execution
Static static RAM 0 Limited to the block Throughout the complete program
External extern RAM 0 Global Throughout the complete program

Automatic Storage Class Specifier

When we declare a variable with auto keyword Storage class specifier it belongs to the automatic storage class. If we do not mention any class specifier to a variable it would also fall in automatic storage class.

The variables of automatic storage class use the RAM memory to execute and they have garbage value by default if we have defined no value. The life of an automatic class storage variable is limited to the execution block of that function in which it has been defined. Once the function execution is completed the compiler destroys the variable.

Example:

#include<stdio.h>
void main()
{
 auto int x; // here x will have a garbage value
 {    auto int x = 1;
      {   auto int x = 2;
                  printf("%d\n",x);
      }
      printf("%d\n",x);
 }
printf("%d\n",x);
getch();
}

Output:

2
1
-23234

Register Storage Class:

When we want a variable to follow the property of a register storage class for that we use the register keyword before the variable declaration. The register is quite similar to auto class specifier the only difference is the register storage class variable uses the register memory instead of RAM. Due to using the Register memory, the register variable acts faster than auto variables. The scope and life of a register variable are limited to the block function.

Example:

#include<stdio.h>
void main()
{
auto int x; // here x will have a garbage value
 {    auto int x = 1;
      {   auto int x = 2;
                  printf("%d\n",x);
      }
      printf("%d\n",x);
 }
printf("%d\n",x);
getch();
}

Output:

2
1
-28760

Static Storage Class:

We use the static keyword to make a variable belong to Static Storage Class. If a variable becomes static by nature it values by default become zero. Though its scope will be limited to the function blocks, its value does not destroy even after the function execution.

It uses the RAM memory to store the variable value. We often use the static storage class specifier when we want the user-defined function local variables not to destroy their value after each function call.

Example:

#include<stdio.h>
void func1()
{
   static int x;
   static int count = 1;
   x+=2;
   printf("The value of x when function call %d time is %d\n",count,x);
   count+=1;
}

void main()
{
   static i; //by default the value of i will become 0
   printf("the value of i is %d\n",i);
   func1();
   func1();
   getch();
}

Output:

the value of i is 0
The value of x when function call 1 time is 2
The value of x when function call 1 time is 4

External Storage Class Specifier:

External Storage is quite similar to the Static Storage Class specifier the only difference is the scope of the variable is global which mean every function can access this variable. We do not use extern keyword to create a variable we just use it to refer the global variable in the function. First, we declare a variable globally and to access that variable we use the extern keyword.

Though we can access a global variable without using an extern keyword so what is the use of extern keyword at all?

We can only use the global variable in our function if it is declared before the function declaration. What if we use to access a global variable which is declared after the function declaration? this will cause an error.

Example:

#include<stdio.h>
void main()
{
   extern int i;
   printf("the value of i is %d\n",i);
   getch();
}
int i=20;

Output:

The value of i is 20

Note: If you try to execute the above program without extern keyword it will print garbage value for i.

People Also Reading:

Leave a Reply

Your email address will not be published. Required fields are marked *