C Storage Classes

In C language, a storage class defines the scope and life time of a variable. There are four typs of storage class in C :
  • auto
  • extern
  • static
  • register

1. auto :

The auto storage class is by default applied to all local variables, means variable declared inside a function without any storage class specification, is by default in auto storage class. The 'auto' storage class can only be used within functions, i.e. local variables. For example :
#include <stdio.h>

void main() {
  int var1;
  // or
  auto int var2;
}
The above example defines two variables with in the same storage class.

2. extern :

The extern storage class is used to give a reference of a global variable that is visible to all the program files. A variable created with extern variable class is an external variable. An external variable is a variable defined outside any function block. By using extern keyword, we can create external variable, and it can be shared with multiple C source files. The syntax is :
extern variable_name = value;
Example :

Filename : myheader.h
extern int num = 10;
Filename : test.c
#include <stdio.h>
#include "myheader.h"

int main() {
  printf("Global variable is : %d\n", num);
}
Output :

Global variable is : 10

3. static :

A static storage class tells the compiler to keep a local variable in existence during the life-time of the program instead of creating and destroying it each time it comes into and goes out of scope. The variable created with static storage class is known as static variable. A static variable is a variable that has been allocated statically, means its lifetime is the entire run of the program. Generally the variables declared inside the functions are destroyed or deleted when function ends. However sometimes we need those variables for some reason. So to this we have to use the keyword static when defining the variable. The syntax for creating a static variable is :
static datatype variable_name;
Example :
static int x = 10;
Now lets see an Example :
#include <stdio.h>

void Myfunc() {
  static int x = 1;
  printf("%x\n", x);
  x++;
}

int main() {
  Myfunc();
  Myfunc();
  Myfunc();

 return 0;
}
Output :

1
2
3

At the above example we can see that the static variable in MyFunc() will not destroyed at end of function, instead it increament itself and print the old value again in next call.

4. register :

The register storage class is used to define local variables that should be stored in a register instead of RAM. The register variables have faster accessibility than a normal variable. The register variable should only be used for variables that require quick access such as counters. The syntax cor creating register variable is :
register int counter;
C example code :
#include <stdio.h>

int main() {
  register int num = 0;

  while(num < 5) {
    printf("%d\n", num+1);
    num++;
  }

  return 0;
}
Output :

1
2
3
4
5


Storage Class Storage Place Default Value Scope Life-time
auto RAM Garbage Value Local Within function
extern RAM Zero Global Till the end of main program, May be declared anywhere in the program
static RAM Zero Local Till the end of main program, Retains value between multiple functions call
register Register Garbage Value Local Within function

Next Topic :