Updated March 16, 2023
Introduction to C Storage Classes
Every value or number needs to be stored in some place for later usage, right? This can be done using variables in C. Variables are storage areas used in our programs. Each variable will be of a specific type like integer, character, and a specific size and layout, depending on their type. Each type of variable should be stored in a specific part of the memory and will have restricted or specific access permissions. Storage Classes in C determines in which part of the memory each variable should be stored, and also it decides the scope (visibility) of the variable. There are four types of storage classes–auto, extern, static and register. One storage specifier can be mentioned along with the variable.
Types of Storage Classes in C
Types of Storage Classes in C are as follows.
1. Automatic Storage Class
All variables declared within a function or block will be stored in an auto specifier by default, even if it is not explicitly defined. The specifier for this storage class is ‘auto’. The scope or visibility of the variables in the automatic storage class is local to the block or function it is defined. The variable will be destroyed once we come out of the function or the block.
This can be explained better with an example. Consider the example given below:
Code:
#include<stdio.h>
int main() {
int i = 2;
{ int i = 4;
printf("%d\n", i);
}
printf("%d\n", i);
}
Output:
Here, a variable I of type integer is declared first with value 2 assigned to it. Next, inside a loop or block again, variable I of the same integer type is declared with value 4 assigned to it. If the storage specifier is not mentioned, by default, it will be taken as auto. The first printf statement mentioned inside the block will print 4 on printing the value of I. Whereas in the second printf statement, which is mentioned outside the block, will print the value of I as 2, the value which is mentioned outside the block. It is better to initial some value to auto variables because there are chances of getting any garbage value sometimes if initialization is not done. This example gives a clear picture of auto variables and about local scope.
2. Register Storage Class
The variables stored in the register storage class will also have local scope, which means it is accessible or visible only in the block in which it is declared. This storage is similar to auto, but the main difference is that auto variables are stored in memory, whereas the register variables are stored in the CPU registers. This is done if we want to access the variable very frequently. These can be used faster. Only a few variables are stored using register specifiers. If there is no space in the register, then it is stored in the memory only. No initial value is assigned to the register variables. Also, & (address-of) operator cannot be used on register variables. For example, variables which are used for counters or similar usage types are stored using a register specifier.
3. Static Storage Class
Variable, may it be global or local, are stored using static specifier in static storage class when the variable needs to be declared once, and the value needs to be retained. When a variable is declared as static, the value will be saved or retained between the function calls. Permanent storage is created, and it is declared only once. When a local variable is declared as static, permanent storage is created for it, and the value is retained every time it is used. Also, as per the scope of the usual local variable, static local variables are also visible only to the function or block where it is defined. When a global variable is declared as static, similar to static local, permanent storage is created, and it is declared only once. But even though it is global, these variables are only visible within the file in which it is defined.
Static variables can be clearly pictured using the below example:
Code:
#include<stdio.h>
int samplefunc() {
static int a = 0;
a = a+2;
return a;
}
int main() {
int result1 = samplefunc();
int result2 = samplefunc();
printf("%d\n", result1);
printf("%d\n", result2);
}
Output:
Here, in the above program, when the samplefunc() is called, the variable a is defined and initialized the first time, and permanent storage is created for it. By the mathematical expression used in the function, the value of a then becomes 2. But when the same samplefunc() is called the second time, variable a is not defined or initialized again; rather, it takes the last retained value and continues with the operation, making the final result as 4. This is the main usage and advantage of static variables.
4. Extern Storage Class
Variable declared as extern depicts that the variable is defined elsewhere in another program. These extern variables are used when we want any variable or function defined in one program to be used in another file too. The variables with the extern specifier are stored in the extern storage class. When the variable is declared as an extern is a program, it specifies the external linkage, and hence it is not defined or initialized again. Storage is allocated only one and also initialized only once. If extern variables are initialized again with another value in the external program, we will get an error stating ‘Redefinition of the variable’.
Extern variables are explained using the below example:
Code:
Prg1.c
int count;
int main() {
count = 10;
}
Prg2.c
extern int count;
int main() {
printf(“%d”, count);
}
Output:
Here, the integer variable count is declared in the first C program (Prg1.c), and inside the main function, it is initialized to value 10. In the second C program, the same count variable is declared using an extern specifier, which specifies that there is external linkage and the value is fetched from the storage, and the value 10 is given to the value count when we print it in the second program. This is the usage of extern variables. Thus, depending on the different purpose, each storage classes are used for appropriate variables, and it is declared with the corresponding specifiers.
Recommended Articles
This is a guide to the C Storage Classes. Here we discuss the basic concept, four different types of Storage Classes in C, respectively. You may also look at the following articles to learn more –