Storage classes in C
In this tutorial, we will be covering storage classes in the C programming language.
In C, to define a variable there is a need to define the storage class of the variable along with the data type of the variable. In case if you do not specify the storage class of a variable in its declaration then the compiler will assume a storage class depending upon the content in which the variable is used.
Thus all the variables have a certain default storage class.
The storage class mainly defines the following characteristics of a variable:
- Storage A variable can be stored either in the memory or in the CPU register. The storage class of the variable mainly tells you where the variable should be stored.
-
Scope: Mainly the scope or visibility of the variable refers to those parts of the program which will be able to recognize the variable. Also, the storage class defines the scope of the variable, i.e in which functions the value of the variable would be available.
-
Default initial value If the initial value of the variable is not specified in the declaration statement, then the storage class mainly tells you that what will be the initial value of the variable.
-
Lifetime The lifetime of the variable is the length of time it retains a particular value. Storage class defines the life of the variable i.e for how long will that variable exist.
The following storage classes are most often used in C programming,
-
Auto(automatic)storage class
-
Extern(External)Storage class
-
Static storage class
-
Register storage class
Automatic storage class: auto
Scope: Variable defined with auto storage class is local to the function block inside which they are defined.
Default Initial Value: Any random value i.e garbage value.
Lifetime: Till the end of the function/method block where the variable is defined.
A variable declared inside a function without any storage class specification is by default an automatic variable. They are created when a function is called and are destroyed automatically when the function's execution is completed. Automatic variables can also be called local variables because they are local to a function. By default, they are assigned garbage value by the compiler.
#include<stdio.h>
void main()
{
int detail;
// or
auto int details; //Both are same
}
In the above example, there are two variables detail and details, where the variable detail is local and by default automatic, whereas the variable details have been explicitly defined as auto.
Both variables are equivalent as far as scope and storage class is concerned.
Features of variables declared with auto storage class:
-
Variable gets stored in the memory.
-
If the variable declared with auto is not initialized at the time of declaration then the initial value of the variable is unpredictable and it is often called a Garbage Value.
-
This variable is local to the block in which it is declared.
-
The variable retains its value till the control remains in the block in which the variable is declared. Suppose, the variable is declared within a function then it will retain its value till the control is within that function. As the execution of the function terminates, the variable gets destroyed.
External Storage Class or Global variable
Scope: Global i.e everywhere in the program. These variables are not bound by any function, they are available everywhere.
Default initial value: 0(zero).
Lifetime: Till the program doesn't finish its execution, you can access global variables.
A variable that is declared outside any function is a Global Variable. Global variables remain available throughout the program execution. By default, the initial value of the Global variable is 0(zero). One important thing to remember about the global variables is that their values can be changed by any function in the program.
#include<stdio.h>
int number; // global variable
void main()
{
number = 10;
printf("I am in main function. My value is %d\n", number);
fun1(); //function calling, discussed in next topic
fun2(); //function calling, discussed in next topic
}
/* This is function 1 */
fun1()
{
number = 20;
printf("I am in function fun1. My value is %d", number);
}
/* This is function 1 */
fun2()
{
printf("\nI am in function fun2. My value is %d", number);
}
I am in function main. My value is 10 I am in function fun1. My value is 20 I am in function fun2. My value is 20
Here the global variable number is available to all three functions and thus, if one function changes the value of the variable, it gets changed in every function.
Note: Declaring the storage class as global or external for all the variables in a program can waste a lot of memory space because these variables have a lifetime till the end of the program. Thus, variables, which are not needed till the end of the program, will still occupy the memory and thus, memory will be wasted.
The variable stored with the extern storage class has the following features:
-
Get stored in the memory.
-
Retains its value till the program execution terminates.
-
Variable will be global i.e visible to all functions
-
If the variable is not initialized in the declared statement then the initial value of the variable will be zero.
The extern keyword
The extern keyword is used with a variable to inform the compiler that this variable is declared somewhere else. The extern declaration does not allocate storage for variables.

The problem when extern is not used
int main()
{
a = 10; //Error: cannot find definition of variable 'a'
printf("%d", a);
}
Example using extern in the same file
int main()
{
extern int x; //informs the compiler that it is defined somewhere else
x = 10;
printf("%d", x);
}
int x; //Global variable x
Static Storage class
Scope: Local to the block in which the variable is defined
Default initial value: 0(Zero).
Lifetime: Till the whole program doesn't finish its execution.
A static variable tells the compiler to persist/save the variable until the end of the program. Instead of creating and destroying a variable every time when it comes into and goes out of scope, static variable is initialized only once and remains in existence till the end of the program.
A static variable can either be internal or external depending upon the place of declaration. Scope of the internal static variable remains inside the function in which it is defined. External static variables remain restricted to the scope of the file in which they are declared.
The variable declared with the static storage class has the following features:
-
Stored in the memory.
-
If not initialized in the declaration statement, the initial value will be zero.
-
Local to the block in which the variable is declared.
-
It retains the value between different function calls. When the function is called the first time the static variable gets created with initial value 0, and then in a subsequent call, it retains its value.
They are assigned 0 (zero) as the default value by the compiler.
#include<stdio.h>
void test(); //Function declaration (discussed in next topic)
int main()
{
test();
test();
test();
}
void test()
{
static int a = 0; //a static variable
a = a + 1;
printf("%d\t",a);
}
1 2 3
Register storage class
Scope: Local to the function in which it is declared.
Default initial value: Any random value i.e garbage value
Lifetime: Till the end of function/method block, in which the variable is defined.
Register variables inform the compiler to store the variable in the CPU register instead of memory. Register variables have faster accessibility than a normal variable. Generally, the frequently used variables are kept in registers. But only a few variables can be placed inside registers. One application of the register storage class can be in using loops, where the variable gets used a number of times in the program, in a very short span of time.
Note: We can never get the address of such variables.
Syntax :
register int number;
Note: Even though we have declared the storage class of our variable number as register, we cannot surely say that the value of the variable would be stored in a register. This is because the number of registers in a CPU is limited. Also, CPU registers are meant to do a lot of important work. Thus, sometimes they may not be free. In such a scenario, the variable works as if its storage class is auto.
The variable declared with the storage class has the features as follows:
-
The variable gets stored in the processor registers if a register is available. In case of non-availability of register, the variable gets stored in the memory and it works as if its storage class is auto. After that as soon as the register becomes available then the storage class of the variable gets converted into a register type.
-
If the variable is not initialized in the declaration statement then the initial value of the variable is zero.
-
The variable is local to the block in which it is declared.
-
The variable retains its value until the control remains in the block in which the variable is declared.
Example: Resgister Storage class
#include<stdio.h>
int main()
{
register int x;
for(x=0;x<5;++x)
printf("%3d",x);
return 0;
}
0 1 2 3 4
Which storage class should be used and when
To improve the speed of execution of the program and to carefully use the memory space occupied by the variables, the following points should be kept in mind while using storage classes:
-
We should use
staticstorage class only when we want the value of the variable to remain the same every time we call it using different function calls. -
We should use
registerstorage class only for those variables that are used in our program very often. CPU registers are limited and thus should be used carefully. -
We should use external or global storage class only for those variables that are being used by almost all the functions in the program.
-
If we do not have the purpose of any of the above-mentioned storage classes, then we should use the automatic storage class.
Frequently Asked Questions
1) What are different storage classes in C?
Four different storage classes in C are as follows:
-
Auto(automatic)storage class
-
Extern(External)Storage class
-
Static storage class
-
Register storage class
2) What do you mean by Storage Class in C?
In C, Storage Classes are mainly used to define the storage of the variable. The features described by the storage class mainly include the scope, visibility, and lifetime that mainly helps us to trace the existence of a particular variable during the runtime of the program
3) What is extern keyword in C?
In C, the extern keyword is a way of explicitly declaring a variable. It mainly forces a declaration without any definition. To define a variable, i.e. to force a definition explicitly is possible in C and is simply done assigning an initialization value to a variable.
4) Which is the faster storage class in C?
In C, the auto variables gets stored in the main CPU memory. Also, the Register variables get accessed very faster rather than the normal variables because they are stored in the register memory rather than the main memory, so the register storage class is faster.
Conclusion
This tutorial was all about storage classes in the C Programming language. We have covered different types of storage classes along with their examples. We have also covered which storage class should be used when.