C Programming Storage Class

Every variable in C programming has two properties: type and storage class. Type refers to the data type of variable whether it is character or integer or floating-point value etc. And storage class determines how long it stays in existence.

There are 4 types of storage class:

  1. automatic
  2. external
  3. static
  4. register

Automatic storage class

Keyword for automatic variable


Variables declared inside the function body are automatic by default. These variable are also known as local variables as they are local to the function and doesn't have meaning outside that function

Since, variable inside a function is automatic by default, keyword auto are rarely used.

External storage class

External variable can be accessed by any function. They are also known as global variables. Variables declared outside every function are external variables.

In case of large program, containing more than one file, if the global variable is declared in file 1 and that variable is used in file 2 then, compiler will show error. To solve this problem, keyword extern is used in file 2 to indicate that, the variable specified is global variable and declared in another file.

Example to demonstrate working of external variable

void Check();
int a=5;    
/* a is global variable because it is outside every function */
int main(){
    return 0;

void Check(){
/*  ----- Variable a is not declared in this function but, works in any function as they are global variable -------  */



Register Storage Class

Keyword to declare register variable


Example of register variable

register int a;

Register variables are similar to automatic variable and exists inside that particular function only.

If the compiler encounters register variable, it tries to store variable in microprocessor's register rather than memory. Value stored in register are much faster than that of memory.

In case of larger program, variables that are used in loops and function parameters are declared register variables.

Since, there are limited number of register in processor and if it couldn't store the variable in register, it will automatically store it in memory.

Static Storage Class

The value of static variable persists until the end of the program. A variable can be declared static using keyword: static. For example:

static int i;

Here, i is a static variable.

Example to demonstrate the static variable

#include <stdio.h>
void Check();
int main(){
void Check(){
    static int c=0;


0      5     10

During first function call, it will display 0. Then, during second function call, variable c will not be initialized to 0 again, as it is static variable. So, 5 is displayed in second function call and 10 in third call.

If variable c had been automatic variable, the output would have been:

0     0     0