What is Storage Class in C?
Storage class is used to define the scope and lifetime of a variable. It tells the compiler where to allocate memory for a variable.
Every variable in C++ has a type and the storage class. The type defines its data types such as int, float, etc. While storage defines two features such as scope and lifetime of a variable.
A lifetime of a variable refers to the period of its activeness and visibility refers to the accessibility of a variable in a program.
C++ program uses the following storage class:
- Automatic
- Register
- Static
- External
- Mutable
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 where they have been declared and not outside them which defines their scope.
These are also referred to as local variables. By default, the variable declared in a program is auto storage variables and if they are not assigned then they hold a garbage value.
auto
keyword is used to defined an auto storage class in a program.
Example:
1 | auto int number; |
1 2 3 4 5 6 | //Example { auto int num; float num = 3.45; } |
The above defines a auto variable that are only accessible within those curly braces.
C++ Program for auto storage class
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | #include <iostream> using namespace std; //user-defined funciton void autoStorage() { // auto variables auto x = 20; auto y = 50; //displaying cout << "x: " << x << " \n"; cout << "y: " << y << " \n"; } int main() { //calling a function autoStorage(); return 0; } |
Output:
1 2 | x: 20 y: 50 |
Now if you try to access the x or y variables through the main function, you will get an error indicating the scope of x and y is within the autoStorage() function only.
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 or something else. A typical application of the register storage class is loop counters, which get used a number of times in a 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 memory instead of RAM memory for quick access.
It also means that the variable size is equal to the register size, usually one word.
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.
Registers are declared as:
1 | register int variable_name; |
Static Storage Class:
The keyword static
is used to declare this type of variables in a c++ 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, a global static variable can be accessed anywhere throughout the program.
The static variable has the default value 0 which is provided by compiler.
NOTE: Every global variable, defined outside functions has the type static automatically. The opposite of static is auto.
C++ program of static storage class
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 <iostream> using namespace std; //global variable static int count = 5; // Function void staticFunc(void) { // local static variable static int temp = 1; temp++; cout << "temp is: " << temp << ", count is: " << count << endl; } int main() { while (count--) { staticFunc(); } return 0; } |
Output:
1 2 3 4 5 | temp is: 2, count is: 4 temp is: 3, count is: 3 temp is: 4, count is: 2 temp is: 5, count is: 1 temp is: 6, count is: 0 |
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.
The main purpose of using extern is that they can be accessed between two different files which are located apart in a large program providing the reference of the variable. Check the program below to understand clearly.
1 | extern int variable_name; |
C++ program for extern storage class
We will create two C++ file and access the variable/function of one file to another with static keyword.
First file: main.cpp
1 2 3 4 5 6 7 8 9 10 11 12 | #include <iostream> int temp; //accessed using extern extern void extern_example(); main() { temp = 10; write_extern(); } |
Second file: test.cpp
1 2 3 4 5 6 7 8 9 10 11 12 | #include <iostream> using namespace std; //this count variable is declared in main.cpp //here it accessed by using extern extern int temp; //some user-defined function void extern_example(void) { cout << "Count is " << temp << endl; } |
Output:
1 | 10 |
As you can see, the variable of main.cpp file is accessed in test.cpp file using extern keyword and extern_example() function of test.cpp file is accessed in main.cpp file.
mutable Storage Class
mutable keyword is used for some specific task such as to modify one or more data members of class/struct through const function. This specifier is applied to only class objects.
If we summarize all of the in a table, we will get,
Storage Class | Lifetime | Visibility | Default Value |
---|---|---|---|
Automatic (auto ) | Function Block | Local | Garbage |
Register (register ) | Function Block | Local | Garbage |
External (extern ) | different program files | Global | Zero |
Static (static ) | Whole Program | Local | Zero |
Mutable (mutable ) | Class | Local | Garbage |