Storage Classes in C

Each variable has a storage class which defines the features of that variable. It tells the compiler about where to store the variable, its initial value, scope ( visibility level ) and lifetime ( global or local ).

There are four storage classes in C.

  • auto
  • extern
  • static
  • register

Let's understand each of these one by one.


Variables which are defined within a function or a block ( block is a section of code which is grouped together. eg.statements written within curly braces constitute a block of code ) by default belong to the auto storage class. These variables are also called local variables because these are local to the function and are by default assigned some garbage value. Since these variables are declared inside a function, therefore these can only be accessed inside that function. There is no need to put 'auto' while declaring these variables because these are by default auto.

Though you have been using these auto(local) variables in your entire C tutorial, let's see one example.

#include <stdio.h>

int sum(int n1, int n2){
  auto int s;        //declaration of auto(local) variable
  s = n1+n2;
  return s;
int main(){
  int i = 2, j = 3, k;
  k = sum(i, j);
  printf("sum is : %d\n", k);
  return 0;
sum is : 5

Here, the variable 's' is declared inside the function 'sum' and thus can be used only inside the function. There was no need to put 'auto' while declaring it.


We write extern keyword before a variable to tell the compiler that this variable is declared somewhere else. Basically, by writing extern keyword before any variable tells us that this variable is a global variable declared in some other program file.

Now let's see what actually happens.

You must be knowing what a global variable is. A global variable is a variable which is declared outside of all the functions. It can be accessed throughout the program and we can change its value anytime within any function as follows.

#include <stdio.h>
int g;
void print(){
  g = 10;
  printf("g = %d\n", g);
int main(){
  g = 7;
  printf("g = %d\n", g);
  return 0;
g = 7
g = 10

Here, g is the global variable defined outside of all the functions. In the main function, its value was assigned as 7 and in the print function as 10.
While declaring a global variable, some space in memory gets allocated to it like all other variables. We can assign a value to it in the same program file in which it is declared as we did in the above example. But what if we want to use it or assign it a value in any other program file.

We can do so by using extern keyword as shown below.


int g = 0;

In the first program file firstfile.c, we declared a global variable g.

Now, we will declare this variable 'g' as extern in a header file firstfile.h and then include it in the second file in which we want to use this variable.


extern int g;

Now in the second program file secondfile.c, in order to use the global variable 'g', we need to include the header file in it by writing #include "firstfile.h". Here we assigned a value 4 to the variable 'g' and thus the value of 'g' in this program becomes 4.


#include "firstfile.h"
  g = 4;
  printf("%d", g);


A variable declared as static once initialized, exists till the end of the program. If a static variable is declared inside a function, it remains into existence till the end of the program and not get destroyed as the function exists (as in auto). If a static variable is declared outside all the functions in a program, it can be used only in the program in which it is declared and is not visible to other program files(as in extern).

Let's see an example of a static variable.

#include <stdio.h>
 static int g = 5;
 void fn(){
 	static int i = 0;
 	printf("g = %d\t", g--);
 	printf("i = %d\n",i++);
int main(){
  while(g >= 2)
  return 0;
g = 5    i = 0
g = 4    i = 1
g = 3    i = 2
g = 2    i = 3

Here, g and i are the static variables in which 'g' is a global variable and 'i' is a local variable. If we had not written static before the declaration of 'i', then everytime the function 'fn()' would have been called, 'i' would have been declared every time with an initial value of 0 and as the function 'fn()' would exit, it would also have got destroyed.


It tells the compiler that the variable will get stored in a register instead of memory (RAM). We can access a register variable faster than a normal variable. Not all the registers defined as register will get stored in a register since it depends on various restrictions of implementation and hardware.

We cannot access the address of such variables since these do not have a memory location as which becomes clear by the following example.

#include <stdio.h>
int main()
    register int n = 20;
    int *ptr;
    ptr = &n;
    printf("address of n : %u", ptr);
    return 0;
prog.c:2:1: warning: return type defaults to 'int' [-Wimplicit-int]
prog.c: In function 'main':
prog.c:5:1: error: address of register variable 'n' requested
ptr = &n
To learn from simple videos, you can always look at our C++ video course on CodesDope Pro. It has over 750 practice questions and over 200 solved examples.

Ask Yours
Post Yours
Doubt? Ask question