Storage classes in C

From C compiler’s point of view, a variable name identifies some physical location within the computer where the string of bits representing the variable’s value is stored. There are basically two kinds of locations in a computer where such a value may be kept— Memory and CPU registers. It is the variable’s storage class that determines in which of these two locations the value is stored.
Moreover, a variable’s storage class tells us:

  1. Where the variable would be stored.
  2. What will be the initial value of the variable, if initial value is not specifically assigned.(i.e. the default initial value).
  3. What is the scope of the variable; i.e. in which functions the value of the variable would be available.
  4. What is the life of the variable; i.e. how long would the variable exist.
    There are four storage classes in C:

  1. Automatic storage class
  2. Register storage class
  3. Static storage class
  4. External storage class

1) Automatic Storage Class

The features of a variable defined to have an automatic storage class are as under:

Storage Memory
Default initial value An unpredictable value, which is often called a garbage value.
Life Till the control remains within the block in which the variable is defined
Scope Local to the block in which the variable is defined.

Following program shows how an automatic storage class variable is declared, and the fact that if the variable is not initialized it contains a garbage value.

#include<stdio.h>
int main() {
	auto int i, j;
	printf("\n%d %d", i, j);
	getchar();
	return 0;
}

2) Register Storage Class

The features of a variable defined to be of register storage class are as under:

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

A value stored in a CPU register can always be accessed faster than the one that is stored in memory. Therefore, if a variable is used at many places in a program it is better to declare its storage class as register. A good example of frequently used variables is loop counters. We can name their storage class as register.

#include<stdio.h>
int main() {
	register int i;
	for (i = 1; i <= 10; i++)
		printf("\n%d", i);
	getchar();
	return 0;
}

Here, even though we have declared the storage class of i as register, we cannot say for sure that the value of i would be stored in a CPU register. Why? Because the number of CPU registers are limited, and they may be busy doing some other task. What happens in such an event… the variable works as if its storage class is auto.

3) Static Storage Class

The features of a variable defined to have a static storage class are as under:

Storage Memory
Default initial value Zero
Life Value of the variable persists between different function calls.
Scope Local to the block in which the variable is defined.
#include<stdio.h>
int main() {
	increment();
	increment();
	increment();
	getchar();
	return 0;
}
increment() {
	static int i = 1;
	printf("%d\n", i);
	i = i + 1;
}

4) External Storage Class

The features of a variable whose storage class has been defined as external are as follows:

Storage Memory
Default initial value Zero
Life As long as the program’s execution doesn’t come to an end.
Scope Global.
#include<stdio.h>
int i; // global variable
int main() {
	extern var; // another global variable, note the extern keyword
	printf("\ni = %d", i);
	increment();
	increment();
	decrement();
	decrement();
	getchar();
	return 0;
}
var = 10;
increment() {
	i = i + 1;
	printf("\non incrementing i = %d", i);
}
decrement() {
	i = i - 1;
	printf("\non decrementing i = %d", i);
}