storage class in c

storage class in c

When we create a variable in c, then two things are always attached with variable data type and storage class. storage class decides the extent (lifetime) and scope (visibility) of the variable in the program.

Every variable gets some physical location from memory where the variable’s value is stored in the form of bits. Storage class defines the storage location of the variable like CPU register or memory besides it also defines the lifetime of the variable in the program.

A storage class helps us to trace the existence of a specific variable during the run-time of the program.

You can also see this article, Memory layout of C Program

We have four different storage class in C


An auto storage class is default class. if we have declared a variable within a function without defining any storage class then variable automatically promoted to auto storage class.

In simple word, we can say local variable (non-static) is an auto variable and scope of the auto variable is inside the function in which it declares and lives till control within the function.

Some properties related to an auto variable

  • scope: Within the block or function in which it declares.
  • life: Live till the control remains in function.
  • storage: auto variable creates in the stack of the process.
  • Default value: Garbage.


When we will write static (prefix) with variable then it alive across the entire run of the program. means the extent of a static variable over the entire run of the program and the scope of a static variable within the module in which it declares.

If we will use the static keyword with a global variable then the scope of a global variable limited to the file in which it declares. in other word we can say that static makes a variable or function private for the file in which it declares.

When we have initialized a static variable then it will be created in the Data Segment (.ds) either it will be created in .bss (block started symbol) of the process.

Note: static variable preserves its previous value and it is initialized at compilation time when memory is allocated. If we do not initialize the static variable, then it’s a responsibility of compiler to initialize it with Zero value.

Some properties related to static variable

  • scope:
    the scope within the function in which declare. But if the global variable is static then the scope of that variable limited to the file in which it declares.
  • life:
    life of static variable whole execution of the program.
  • storage:
    if the static variable is initialized then stored in .ds otherwise stored in .bss.
  • Default value:
    the default value of the static variable is Zero.


The extern storage class only declare a variable and it used to give the reference of the global variable that is accessed by all files of the program. Simply extern says to the compiler that this variable is defined in elsewhere in the program, it only points to the already defined variable in the program.

A variable did not get any memory until you defined it. When you have declared a variable, then you just give the information of the variable to the compiler, but there is no memory reserve it. You can declare a variable multiple time in your program but define only once.

Suppose a project contains two file Display.c and Calculation.c . All calculation related function defined in calculation file and display related function defined in the display file.

If you compile the above project then you will get linker error “undefine reference to gData” because, at the linking time, linker did not resolve the address of gData.

So how we can solve this problem?

Yes, you are thinking in the right way using the extern keyword, we can solve this problem. If we declare gData using the extern keyword in Display file then the linker will resolve the address of gData.

Some properties related to the global variable

It accesses by all function.
life of the global variable is the throughout of the execution of the program.
If the global variable is initialized then stored in .ds otherwise stored in .bss.
Default value:
The defaults value of the global variable is Zero.

Note: we can declare a variable multiple time in a program but define only a single time.  


You can also see, Local, Static and Global variables in C


Register variable stored in CPU register instead of memory and its properties is generally similar to the local variable. Accessing of the register is faster than the memory. So generally, the register variable is used in looping.
A register keyword only gives the indication to the compiler to store this variable in the register instead of RAM, But it totally depends on the compiler. The compiler decides where to put the variable in register or RAM.

Note: We can not use & and * operator with a register variable because access the address of the register variable is invalid.

error: address of register variable ‘Data’ requested.



  1. __file1.c___


    int x;

    int main()
    x =10;
    return 0;


    extern int x;

    int main()
    return 0;}

  2. int Add(void)
    static int iSum = 1; //stored in .ds and scope within the Add function
    static int iData; //stored in .bss and scope within the Calculation.c “i think here scope within the Add function”


Leave a Reply