Storage Classes in C are used to define the storage and scope of a variable in a C program. C language has four types of storage classes, namely auto, static, register, and extern. These storage classes are used to control the visibility, life span, and memory allocation of the variables. In this article, we will discuss each of the storage classes in detail with examples.
Auto Storage Class
Auto storage class refers to an automatic variable. By default, all local variables in a C program are automatic variables. The scope of an auto variable is within the function block and cannot be accessed outside the function. Auto variables are declared at the beginning of the function. Once the function is called, the memory is allocated, and when the control comes outside the function, the memory of auto variables is deallocated. By default, the initial value of auto variables is garbage values with no meaning.
Declaration syntax of an auto variable is:
cint a; //by default, it is an auto variable.
float b;
This is similar to prefixing the keyword auto before the data types.
cauto int a;
auto float b;
Example Program Using Automatic Variables.
c#include <stdio.h>
void main()
{
int a = 5; //auto variable by default
auto b = 10;
printf("a = %d \n", a);
printf("b = %d \n", b);
}
Output:
cssa = 5
b = 10
Static Storage Class
Static variables are declared with the keyword static. The static variable's lifetime is throughout the program run-time. The static variable declared inside the function cannot be accessed outside the function. Suppose the static variables are declared inside the function. Once the function is called, a static variable's memory is allocated, and it won't be deallocated when the control comes out of the function. Therefore, the current value of a static variable can be accessed in the following function call. Static variables must be initialized before the program execution; otherwise, the initial default value will be zero. Static variables can be declared globally and accessed throughout the program.
Declaration syntax of a static variable is:
cstatic data-type variable_name = value_initialization;
Example:
cstatic int sum =0;
Example Program Using Static Variables.
c#include <stdio.h>
int calc()
{
static int sum = 2;
sum++;
return sum;
}
void main()
{
printf("sum = %d \n", calc());
printf("sum = %d \n", calc());
}
Output:
pythonsum = 3
sum = 4
In the above-given program, the value of a static variable is initialized to the value 2. If the static variable is not initialized in the program, then by default, the initial value will be assigned to zero. The static variable is local to the function in which it is defined. Once the function is called, the value will be incremented by one that is the initial value 2 + 1 = 3 and print the value 3. Once the control is out of the function, the memory will not be deallocated; hence, value 3 is stored. In the next function call, the value 3 will be incremented by one and print the new value that is 4. If the variable were not declared static, then for both the function call, the sum value would be the same, that is, sum = 3 for both the function call.
Register Storage Class
Variables declared with the keyword register are register variables. The scope of register variables is within the block in which they are defined. The use of register keyword is optional, and it is up to the compiler to decide whether or not to store a variable in a register.
For example, the following code declares a register variable num
and initializes it with the value 10:
c#include <stdio.h>
int main() {
register int num = 10;
printf("The value of num is %d\n", num);
return 0;
}
In this case, the compiler may decide to store num
in a register for faster access. However, there is no guarantee that num
will actually be stored in a register, since it depends on the specific compiler and platform being used.
It's worth noting that the use of register
keyword has become less common in modern programming, as compilers have become more sophisticated at optimizing code without explicit hints. In many cases, the compiler may ignore the register
keyword altogether and choose the optimal storage location for a variable based on its usage patterns.
In summary, register
is a keyword used to suggest to the compiler that a variable should be stored in a CPU register for faster access. However, its use is optional, and the compiler may choose to ignore it in favor of its own optimization strategies.