C Storage Class
In C, storage class determines the scope, visibility, and life of a variable. A storage class specifies the location of a variable and the extent of its existence within a program. In C, there are four storage classes: auto
, register
, static
, and extern
.
In this article, we will explore the different storage classes in C, provide examples to demonstrate their use, and show the outputs of the examples.
What is Storage Class?
A storage class in C specifies the location of a variable and the extent of its existence within a program. It determines the scope, visibility, and life of a variable. In C, there are four storage classes: auto
, register
, static
, and extern
.
auto
The auto
storage class is the default storage class for all variables declared within a function. Variables declared with the auto
storage class are automatically created when the function is called and are destroyed when the function returns. They have local scope and are only visible within the function in which they are declared.
Example 1: auto
Storage Class
The following example demonstrates the use of the auto
storage class:
1#include <stdio.h> 2 3void func() 4{ 5 auto int num = 5; 6 printf("Value of num in func(): %d\n", num); 7} 8 9int main() 10{ 11 func(); 12 return 0; 13}
The output of this program is:
1Value of num in func(): 5
In this example, the variable num
is declared with the auto
storage class within the function func
. Since num
is declared with the auto
storage class, it is automatically created when the function is called and is destroyed when the function returns.
register
The register
storage class is used to declare variables that are stored in the CPU register instead of in memory. This can increase the performance of a program by reducing the amount of memory accesses required. However, the number of variables that can be stored in the CPU register is limited, so the use of the register
storage class should be used with caution.
Example 2: register
Storage Class
The following example demonstrates the use of the register
storage class:
1#include <stdio.h> 2 3int main() 4{ 5 register int num = 5; 6 printf("Value of num: %d\n", num); 7 num++; 8 printf("Value of num: %d\n", num); 9 return 0; 10}
The output of this program is:
1Value of num: 5 2Value of num: 6
In this example, the variable num
is declared with the register
storage class. This means that the compiler is asked to store num
in a register, which is a fast memory location inside the CPU, instead of storing it in the RAM. The value of num
is incremented and printed twice, demonstrating that it is stored in a register.
Note that the use of the register
storage class is a request to the compiler, not a requirement. The compiler may choose to ignore this request and store the variable in RAM if it determines that it is not appropriate to store it in a register.
static
The static
storage class is used to declare variables that have a static extent. Variables declared with the static
storage class have a lifetime that extends throughout the entire program, but are only visible within the function in which they are declared.
Example 2: static
Storage Class
The following example demonstrates the use of the static
storage class:
1#include <stdio.h> 2 3void func() 4{ 5 static int num = 5; 6 printf("Value of num in func(): %d\n", num); 7 num++; 8} 9 10int main() 11{ 12 func(); 13 func(); 14 func(); 15 return 0; 16}
The output of this program is:
1Value of num in func(): 5 2Value of num in func(): 6 3Value of num in func(): 7
In this example, the variable num
is declared with the static
storage class within the function func
. Since num
is declared with the static
storage class, its value is retained between function calls. This means that each time the function func
is called, the value of num
is incremented and its value is not reset to 5.
extern
The extern
storage class is used to declare variables that are defined in another source file. Variables declared with the extern
storage class have global scope and are visible throughout the entire program.
Example 4: extern
Storage Class
The following example demonstrates the use of the extern
storage class:
1#include <stdio.h> 2 3int num; 4 5void func() 6{ 7 extern int num; 8 printf("Value of num in func(): %d\n", num); 9 num++; 10} 11 12int main() 13{ 14 num = 5; 15 func(); 16 func(); 17 func(); 18 return 0; 19}
The output of this program is:
1Value of num in func(): 5 2Value of num in func(): 6 3Value of num in func(): 7
In this example, the variable num
is declared with the extern
storage class in both the main function and the function func
. The variable num
is defined in the main function, which means it has global scope and is visible throughout the entire program. The function func
accesses the global num
variable and increments its value each time it is called.