Variables point to a memory location that has an address and a value stored. Other than this value, a variable also has a storage class. The C storage classes decides the characteristics of a variable during the execution of a program – the characteristics are storage location, initial value stored, scope, the lifetime of the variable and linkage.
To understand storage classes we must first discuss these characteristics.
A variable can be stored in memory or the CPU. A storage class decides the location of the value that gets stored in a variable. During its lifetime, a variable holds two things – an address to the memory location and a value of its declared data type.
Every variable is assigned a value by the user or the system assigns a default value to the variable because any variable that is not initialized, does not occupy a space in the memory.
The scope of the variable decides which different sections of the program can access the variable. There is many types of scope for a variable – block level scope, function level scope, function prototype level scope and source file level scope.
Lifetime of a Variable
The lifetime of the variable tells a programmer, how long the variable is going to hold its storage space and memory address during the execution of the program. After the lifetime of a variable, all resources should be accessible to other components of a program.
The linkage tells us whether a variable is public or private. If the variable is sharable to other blocks or functions then it is public, otherwise, it is private.
There are three types of linkage – no linkage, internal linkage and external linkage. The linkage is connected to the scope of a variable. The block level scope, function level scope and the function prototype level scope does not share variables. So, there is no linkage.
The file level or program level scope can have internal or external linkage. The use of static keyword means that the file level scope has internal linkage. Variable using extern keyword means that its an external linkage.
Types of C Storage Classes
There are four storage classes
Automatic Storage Class
The automatic storage class variables are declared using keyword – auto. If you do not declare it as auto or some other C storage classes member, by default it is auto storage class member because all variables are stored in memory.
When you create a new create a new auto storage class variable – a default garbage value is assigned automatically.
To declare a variable as a member of automatic storage class use following syntax.
auto int number;
number = 100;
Example Program 1
Register Storage Class
Variables in this class are declared using the keyword – register. The variable is stored in a register, but that is not guaranteed because the storage space of CPU registers are limited. In case, registers are not available, register storage class variables treated as auto class variables and stored in memory.
The default initial value of register class is a garbage value. Each time you run the program containing register class variable a new garbage value is assigned automatically.
Since register storage class variables are stored in a register, you cannot expect to use the memory reference operator on them. It will produce errors.
The scope of the register class variable is local to its block and no linkage. Its lifetime is till the block of code is running during program execution.
To declare a variable as register storage class type use following syntax.
register int demo_num;
Example Program 2
Output – 2
Static Storage Class
The static variables do not get updated when they are outside of the scope of their block. Normally you declare the static storage class variable only once during its lifetime.
They jump between function to function updating the value of the variable. Static variables are stored in a memory and by default they get a value of 0, ff a user did not initialize the static variable. The scope of the static variable is local to its block, function or function prototype or file level.
The static variable does not get destroyed immediately and they jump from function to function call, depending on how many times the function has been called.
To declare a variable static using the keyword – static.
static int length;
Example Program 3
/* function call */
/* function call */
/* function call */
int length = 0;
static int width;
/* static integer automatically initialized to 0 */
printf(" Current iteration !\n\n");
printf("Length = %d\t Width = %d\n\n", length, width);
Output – 3
External Storage Class
There is another C storage class that connects variables within a C program. The external storage class variables can be used outside of the program or file. They have an external or internal linkage. You must declare these variables using extern keyword. The external storage class variables are stored in memory.
The scope of external storage class variable is global and it can be used by many blocks, functions or files in a C program. In this way, the lifetime of the external storage class variable is as long as the program is running.
The external variable has two cases
- A global variable without extern keyword.
- A global variable with extern keyword.
A Global Variable without extern keyword
If you declare a variable outside of main, it is external and initialized to 0 by default. These variables cannot be used outside of the current source file.
will print a value of 0.
A Global Variable with extern Keyword
To create an external storage class variable use the following syntax.
extern int x;
The above will only give an error because it does not do anything in memory.
Example Program 4
extern int x;
x = 39;
Output – 4
A summary of all C storage classes in C programming language with each character is given below.
When to use a Storage Class ?
The usage of C storage classes depends on the programmer. If the program has constraints for which a storage class can solve the problem, then you should use a storage class variable.
Here is a list of reasons for using a storage class.
- When you want to use a variable that keeps the values intact between functions then use a static variable. This is a case of internal linkage.
- Register storage class variables are fast because they directly manipulated by a CPU, so it dies not go through the memory fetch cycles. You must use them wisely.
- When you have variables that are shared between files in the same program, an external storage class variable is most suitable kind of variable type for this.
- There are so many scenarios where a C programming storage class can be useful and solve important programming problems. It helps programmer build a useful and efficient C programs.
Balagurusamy, E. 2000. Programming in ANSI C. Tata McGraw-Hill Education,.
Brian W. Kernighan, Dennis M. Ritchie. 1988. C Programming Language, 2nd Edition. Prentice Hall.