When we declare a variable in c, two things are always attached to the variable, data type and storage class. Storage class defines the extent (lifetime), default value 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 beside 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.

Now there are  four different type of storage class:

Auto

If we have declared a variable within a function without defining any storage class then the variable automatically promoted to auto storage class. 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.

  • 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.

Static

When we declare 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 declare static variable globally then the scope of global variable limited to the file in which it declares. in another word we can say that static makes a variable or function private for the file in which it declared. When we have initialized a static variable then it will be creating in Data Segment (.ds) either it will be creating 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.

FILE: static.c

  • 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 declared.
  • 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: Default value of the static variable is 0.

Extern

The extern storage class only declare a variable and use to give the reference of a global variable that is accessible by all files of the program. Simply extern says to the compiler that this variable is defined elsewhere in the program, it only points to the already defined variable in the program. The only declaration does not allocate any memory until you defined it. We can declare a variable multiple time in our program but can 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.

File: Calculation.c

File: Display.c

If we compile the above project then will get linker error “undefined reference to gVar” because, at the linking time, linker did not get the address of gVar.

We can solve this: If we declare gVar using the extern keyword in Display file then the linker will resolve the address of gVar.

File: Display.c

  • Scope: It accesses by all function.
  • Life: the life of the global variable is the throughput of the execution of the program.
  • Storage: 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.

Register

 Register variable stores in CPU register instead of memory and it’s generally similar to the local variable. Accessing the register is faster than the memory. So generally, 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’t use ‘&’ and ‘*’ operator with register variable because access the address of the register variable is invalid.

error: address of register variable ‘Data’ requested.