C Storage Classes
Storage classes are characteristics of variables and functions in C programming that specify how memory is allotted for them, how long they last, and how easily they may be accessed within the program. They establish the scope, linkage, and storage duration of an identifier. The storage class of a variable provides information about its scope (where its value is accessible), life (how long it exists), default initial value, and storage location.
There are four main storage class specifiers available in C:
- auto
- register
- static
- extern
Below is an explanation of each:
Automatic (auto)
- Storage: Recall. The system allots memory for auto variables inside a block when it is entered.
- The default starting value: A trash value is a value that is uncertain.
- Scope: The block where the variable is defined locally. A block’s defined identifiers are specific to that block.
- Life: Until the variable is defined and the control is still inside the block. The system releases the memory when the block is exited.
- By default, variables specified inside function bodies are auto. Since the auto keyword is the default, it is rarely used explicitly.
Register (register)
- Storage: CPU registers. For faster access, the compiler is advised to place the variable in a CPU register.
- Default initial value: Garbage value is the default beginning value.
- Scope: The block where the variable is defined locally.
- Life: Until the variable is defined and the control is still inside the block.
Static (static)
- Memory is used for storage.
- Zero is the default starting value.
- Scope: Local to the block where the variable is defined (or, if it is defined outside of a function, local to the file).
- Life: When a function is called or a block is re-entered, the value of the variable remains unchanged. Static variables can be used as global variables or as local variables in functions and files.
External (extern)
- Memory (usually global memory) is used for storage.
- Zero is the default starting value for variables defined outside of any function.
- Scope: Global, available in all functions of the program, and able to be shared between two or more files. An extern variable is one that is defined outside of any block or function and is not static. A variable or function that is defined in another file or elsewhere in the current file can be declared using extern.
For a variable in C to be completely defined, it must specify its type and storage class. The compiler uses the context to determine a default if the storage class is not given.
Example program: It illustrates these storage classes is provided here:
#include <stdio.h>
// declaring the variable which is to be made extern
// an initial value can also be initialized to x
int x; // Implicitly extern, can be initialized here
void autoStorageClass()
{
printf("\nDemonstrating auto class\n\n");
// declaring an auto variable (simply
// writing "int a=32;" works as well)
auto int a = 32; // 'auto' keyword is optional here
// printing the auto variable 'a'
printf("Value of the variable 'a'"
" declared as auto: %d\n",
a);
printf("--------------------------------");
}
void registerStorageClass()
{
printf("\nDemonstrating register class\n\n");
// declaring a register variable
register char b = 'G'; // Suggests storage in a CPU register
printf("Value of the variable 'b'"
" declared as register: %c\n",
b);
printf("--------------------------------");
}
void externStorageClass()
{
printf("\nDemonstrating extern class\n\n");
// Variable x is declared outside any function, making it extern
// It is accessible here because of its extern linkage
printf("Value of the variable 'x'"
" declared as extern: %d\n",
x); // Accessing the global variable x
printf("--------------------------------");
}
void staticStorageClass()
{
printf("\nDemonstrating static class\n\n");
// declaring a static variable
static int p = 10; // Initialized only once, retains value
// Loop to show value retention
for (int i = 0; i < 3; i++) {
p++; // Increments 'p' on each call (or iteration if called multiple times)
printf("The value of static variable 'p', "
"in %d iteration is %d\n",
i, p);
}
printf("\n Loop ended:\n");
printf("--------------------------------");
}
int main()
{
printf("A program to demonstrate"
" Storage Classes in C\n\n");
// To demonstrate auto Storage Class
autoStorageClass();
// To demonstrate register Storage Class
registerStorageClass();
// Assign a value to the extern variable before demonstrating it
x = 50;
// To demonstrate extern Storage Class
externStorageClass();
// To demonstrate static Storage Class
// Calling staticStorageClass() multiple times would show 'p' retaining its value
staticStorageClass();
staticStorageClass(); // Call again to show retention
// exiting
printf("\n\n Storage Classes demonstrated");
return 0;
}
Output:
A program to demonstrate Storage Classes in C
Demonstrating auto class
Value of the variable 'a' declared as auto: 32
--------------------------------
Demonstrating register class
Value of the variable 'b' declared as register: G
--------------------------------
Demonstrating extern class
Value of the variable 'x' declared as extern: 50
--------------------------------
Demonstrating static class
The value of static variable 'p', in 0 iteration is 11
The value of static variable 'p', in 1 iteration is 12
The value of static variable 'p', in 2 iteration is 13
Loop ended:
--------------------------------
Demonstrating static class
The value of static variable 'p', in 0 iteration is 14
The value of static variable 'p', in 1 iteration is 15
The value of static variable 'p', in 2 iteration is 16
Loop ended:
--------------------------------
Storage Classes demonstrated
This program demonstrates how to declare and use variables with the auto, register, extern, and static storage classes. AutoStorageClass is local to the auto variable a. Local to registerStorageClass is the register variable b. In externStorageClass, the extern variable x is accessible after being declared globally. The function is called twice in main to show that the static variable p in staticStorageClass maintains its value between calls. Keep in mind that each time the function is run, the output for the static variable will show that it is increasing from its previous value (10).
Click here to know about C Functions