Storage Classes - C Tutorial

Storage Classes

Storage class specifiers in C language tells the compiler where to store a variable, how to store the variable, what is the initial value of the variable and life time of the variable.

There are four storage classes in C:

  • Auto storage class
  • Register storage class
  • Static storage class
  • Extern storage class

Auto (Automatic) Storage Class

A variable defined within a function or block with auto specifier belongs to automatic storage class. All variables defined within a function or block by default belong to automatic storage class if no storage class is mentioned.

Storage Memory
Default initial value Garbage value
Scope Local to the block in which the variable is defined
Life Till the control remains within the block in which the variable is defined

Register storage class

Register variable inform the compiler to store the variable in CPU register instead of memory. Register variable has faster access than normal variable. Frequently used variables are kept in register. Only few variables can be placed inside register.

Storage CPU registers
Default initial value Garbage value
Scope Local to the block in which the variable is defined
Life Till the control remains within the block in which the variable is defined

Static storage class

The static specifier gives the declared variable static storage class. Static variables can be used within function or file.Unlike global variables, static variables are not visible outside their function or file, but they maintain their values between calls.

Storage Memory
Default initial value Zero
Scope Local to the block in which the variable is defined
Life Value of the variable persists between different function calls

Extern (External) storage class

The extern storage class is used to give a reference of a global variable that is visible to ALL the program files. When you use 'extern', the variable cannot be initialized however, it points the variable name at a storage location that has been previously defined.

Storage Memory
Default initial value Zero
Scope Global
Life As long as the program’s execution doesn’t come to an end