Every variable has a storage class that defines the characteristics of the variable. It tells the compiler where to allocate memory for a variable, it also determines the scope of the variable, visibility, and lifetime of a variable.
There are four types of storage classes:
- Automatic
- External
- Static
- Register
Automatic(auto) Storage class:
A variable defined within a function or block with an auto specifier belongs to the automatic storage class. Auto variables can only be accessed within the block/function they have been declared and not outside them (which defines their scope).
We can also say that these are the local variables. By default, the variable declared in a program is auto storage variables. By default, they are assigned the garbage value whenever they are declared.
auto
keyword is used to define an auto storage class in a program.
Example:
auto int number;
Program for auto storage class in C
Example: The following program demonstrates the use of the auto storage class. The program shows how the same variable name can be used at variable blocks in a program with different values.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | #include <stdio.h> void autoStorageClass() { // declaring an auto variable auto int x = 6; // printing the auto variable printf("Variable x 1st: %d\n", x); { auto int x = 12; //Displaying the variable printf("Variable x 2nd: %d\n", x); } } int main() { // calling auto storage class autoStorageClass(); return 0; } |
Output:
Variable x 1st: 6
Variable x 2nd: 12
External(extern) Storage Class:
Extern stands for external storage class. The principal use of extern is to specify that a variable is declared with external linkage elsewhere in the program. In general, it is used to declare a variable to be used in a module that is not the one in which the corresponding variable is defined.
extern
keyword is used to define the global variable whose scope is within the whole program, which means these variables are accessible throughout the program. That means this variable can be used anywhere in the program and its value can be manipulated from anywhere necessary.
The main purpose of using extern variables is that they can be accessed between two different files which are located apart in a large program providing the reference of the variable.
extern int variable_name;
Program for extern storage class in C
Example: Consider this main.c where a variable with extern is declared.
1 2 3 4 5 6 7 8 9 10 | #include <stdio.h> extern i; main() { printf("The value of the external storage: %d\n", i); return 0; } |
Second File: Second.c is saved where the the value of the variable above is declared.
1 2 | #include <stdio.h> i=48; |
Output:
1 | The value of the external storage: 48 |
Static Storage Class:
This is another storage class that is declared as static
in a program. The name static is given to variables that can hold their values between calls of a function. They are allocated only once and their values are preserved between any number of function calls.
Space is allocated for static variables in the program code itself and hence no new memory is allocated to it as they are not re-declared in the program.
Their scope is local to the function where they are defined. Also, global static variables can be accessed anywhere throughout the program
NOTE: Every global variable, defined outside functions has the type static automatically. The opposite of static is auto.
Program for static storage class in C
You can see in the program that the two variables are declared, one is global(count) and another is local(temp). And both of these value does not reset no matter how many times you call the function to start from the beginning.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | #include <stdio.h> //global static variable static int count = 2; //function void iteration(void) { //local static variable static int temp = 10; temp++; printf("temp: %d\tcount: %d\n", temp, count); } main() { while (count < 5) { //calling function iteration(); count++; } return 0; } |
Output:
1 2 3 | temp: 11 count: 2 temp: 12 count: 3 temp: 13 count: 4 |
Register Storage class:
Register storage class is only for those variables that are being used very often in a program. The reason is, there are very few CPU registers at our disposal and many of them might be busy processing something else. A typical application of the register storage class is loop counters, which get used a number of times in a program.
Registers are mostly used for the quick access of variables in a program. The keyword register is used to declare a register storage class. The variables declared using the register storage class have lifespan throughout the program.
Just like auto storage, it has the scope to a limited block where it is used. The difference is that the variables declared with a register keyword are stored in the register instead of RAM memory.
Note: We cannot get the memory location when dealing with the CPU register. If you use pointers to access the memory location, the program will through an error.
It does not have any default value.
Registers are declared as:
register int variable_name;