Storage class of a variable defines the lifetime and visibility of a variable. Lifetime means the duration till which the variable remains active and visibility defines in which module of the program the variable is accessible. There are five types of storage classes in C++. They are:
- Automatic
- External
- Static
- Register
- Mutable
Storage Class | Keyword | Lifetime | Visibility | Initial Value |
---|---|---|---|---|
Automatic | auto | Function Block | Local | Garbage |
External | extern | Whole Program | Global | Zero |
Static | static | Whole Program | Local | Zero |
Register | register | Function Block | Local | Garbage |
Mutable | mutable | Class | Local | Garbage |
1. Automatic Storage Class
Automatic storage class assigns a variable to its default storage type. auto keyword is used to declare automatic variables. However, if a variable is declared without any keyword inside a function, it is automatic by default. This variable is visible only within the function it is declared and its lifetime is same as the lifetime of the function as well. Once the execution of function is finished, the variable is destroyed.
Syntax of Automatic Storage Class Declaration
datatype var_name1 [= value]; or auto datatype var_name1 [= value];
Example of Automatic Storage Class
auto int x; float y = 5.67;
2. External Storage Class
External storage class assigns variable a reference to a global variable declared outside the given program. extern keyword is used to declare external variables. They are visible throughout the program and its lifetime is same as the lifetime of the program where it is declared. This visible to all the functions present in the program.
Syntax of External Storage Class Declaration
extern datatype var_name1;
For example,
extern float var1;
Example of External Storage Class
Example 1: C++ program to create and use external storage.
File: sub.cpp int test=100; // assigning value to test void multiply(int n) { test=test*n; }
File: main.cpp #include<iostream> #include "sub.cpp" // includes the content of sub.cpp using namespace std; extern int test; // declaring test int main() { cout<<test<<endl; multiply(5); cout<<test<<endl; return 0; }
A variable test is declared as external in main.cpp. It is a global variable and it is assigned to 100 in sub.cpp. It can be accessed in both files. The function multiply() multiplies the value of test with the parameter passed to it while invoking it. The program performs the multiplication and changes the global variable test to 500.
Note: Run the main.cpp program
Output
100 500
3. Static Storage Class
Static storage class ensures a variable has the visibility mode of a local variable but lifetime of an external variable. It can be used only within the function where it is declared but destroyed only after the program execution has finished. When a function is called, the variable defined as static inside the function retains its previous value and operates on it. This is mostly used to save values in a recursive function.
Syntax of Static Storage Class Declaration
static datatype var_name1 [= value];
For example,
static int x = 101; static float sum;
4. Register Storage Class
Register storage assigns a variable’s storage in the CPU registers rather than primary memory. It has its lifetime and visibility same as automatic variable. The purpose of creating register variable is to increase access speed and makes program run faster. If there is no space available in register, these variables are stored in main memory and act similar to variables of automatic storage class. So only those variables which requires fast access should be made register.
Syntax of Register Storage Class Declaration
register datatype var_name1 [= value];
For example,
register int id; register char a;
Example of Storage Class
Example 2: C++ program to create automatic, global, static and register variables.
#include<iostream> using namespace std; int g; //global variable, initially holds 0 void test_function() { static int s; //static variable, initially holds 0 register int r; //register variable r=5; s=s+r*2; cout<<"Inside test_function"<<endl; cout<<"g = "<<g<<endl; cout<<"s = "<<s<<endl; cout<<"r = "<<r<<endl; } int main() { int a; //automatic variable g=25; a=17; test_function(); cout<<"Inside main"<<endl; cout<<"a = "<<a<<endl; cout<<"g = "<<g<<endl; test_function(); return 0; }
In the above program, g is a global variable, s is static, r is register and a is automatic variable. We have defined two function, first is main() and another is test_function(). Since g is global variable, it can be used in both function. Variables r and s are declared inside test_function() so can only be used inside that function. However, s being static isn’t destroyed until the program ends. When test_function() is called for the first time, r is initialized to 5 and the value of s is 10 which is calculated from the statement,
s=s+r*2;
After the termination of test_function(), r is destroyed but s still holds 10. When it is called second time, r is created and initialized to 5 again. Now, the value of s becomes 20 since s initially held 10. Variable a is declared inside main() and can only be used inside main().
Output
Inside test_function g = 25 s = 10 r = 5 Inside main a = 17 g = 25 Inside test_function g = 25 s = 20 r = 5
5. Mutable Storage Class
In C++, a class object can be kept constant using keyword const. This doesn’t allow the data members of the class object to be modified during program execution. But, there are cases when some data members of this constant object must be changed. For example, during a bank transfer, a money transaction has to be locked such that no information could be changed but even then, its state has be changed from – started to processing to completed. In those cases, we can make these variables modifiable using a mutable storage class.
Syntax for Mutable Storage Class Declaration
mutable datatype var_name1;
For example,
mutable int x; mutable char y;
Example of Mutable Storage Class
Example 3: C++ program to create mutable variable.
#include<iostream> using namespace std; class test { mutable int a; int b; public: test(int x,int y) { a=x; b=y; } void square_a() const { a=a*a; } void display() const { cout<<"a = "<<a<<endl; cout<<"b = "<<b<<endl; } }; int main() { const test x(2,3); cout<<"Initial value"<<endl; x.display(); x.square_a(); cout<<"Final value"<<endl; x.display(); return 0; }
A class test is defined in the program. It consists of a mutable data member a. A constant object x of class test is created and the value of data members are initialized using user-defined constructor. Since, b is a normal data member, its value can’t be changed after initialization. However a being mutable, its value can be changed which is done by invoking square_a() method. display() method is used to display the value the data members.
Output
Initial value a = 2 b = 3 Final value a = 4 b = 3