C++ Storage Class

In C++, A Storage Class is defined as visibility(accessibility of the variable or function from the different modules of the program), lifetime(time till which the variable or function remains active), and scope of a variable or function. 

This basically helps in tracking the variable during the runtime of the program. Every variable in c++ has two properties – DataType and Storage Class. DataType stores the type of data stored in the variable, and storage class stores two properties – lifetime and scope. 

If we don’t assign a storage class to a variable, then the compiler automatically assigns a storage class to the variable. 

The storage class of a variable provides information about the initial default value, scope of the variable, lifetime, and memory location of the variable. 

Syntax to initialize a variable with storage class –

Storage_class Var_data_type Var_name 

In C++, we have five different types of storage classes – 

  • Auto 
  • Register 
  • Static 
  • Extern 
  • mutable 

Let’s discuss each one of them in detail – 

  • Auto – The Auto storage class is the default storage class for the variables declared inside any function block. 
    Scope – The scope of this keyword is within the block, in which it is declared. If we want to access an auto variable outside the declared block, then we need to use pointers for the same. We need to point to the memory locations in which the auto variable is located. 
    Lifetime – The lifetime of an auto variable is the same as that of the function or block in which it is declared. Once a function or block is destroyed, the auto variable is also destroyed. 

Syntax:

auto datatype var_name1 = value;

Example Code-

C++ Code

#include<iostream> 
using namespace std; 
int main() { 
auto a = 56; 
auto b = 9.6; 
auto c = "TakeUForward"; 
auto d = 'i'; 
cout << a << endl; 
cout << b << endl; 
cout << c << endl; 
cout << d << endl;
} 

Output:

56
9.6
TakeUForward
i

  • Register – As per the name, the register storage class stores the register variables. It is almost the same as the auto storage class, but in this, the compiler stores the data in a vacant register(if present) in the microprocessor of the computer. If a free register is not present, then we store the data in the memory locations. The operations performed on the register class variables are fast because they are stored in the registers (the processing speed of the registers is equivalent to that of the processor, hence the operations are faster than that of main memory). 
    Scope – The Scope of the keyword is local to the block. Its address can’t be accessed by using pointers. 
    Lifetime – Its lifetime is the same as that of the function or block in which it is declared. Once the function is destroyed, the register variable is also destroyed. 

The maximum size of the register variable is equal to that of the size of a register(approx 1 word). 

Syntax:

register data_type var_name1 = value; 

Code:

C++ Code

#include<iostream> 
using namespace std; 
int a; 
int main() { 
register int a = 5; 
cout << "Value of register variable : " << a << endl; 
} 

Output:

Value of register variable: 5

  • Static – It is used to declare static variables (which retain their last value even when they are not in scope). These variables are initialized only once and terminate only on the termination of the complete program, not a particular block of code. The memory is allocated only once to the variable and by default, the compiler assigns it the value 0. 
    Scope – The scope of the static variables is within the block, in which it is declared. However, they can be accessed outside their scope as well using the concept of pointers given here by pointing to the very exact memory location where the variables reside. 
    Lifetime – These variables terminate only on the termination of the complete program, not a particular block of code. 

Syntax:

static data_type var_name1 = value

Code:

C++ Code

#include<iostream> 
using namespace std; 
int a; 
int main() { 
static int a = 5; 
a++;
cout << a << endl; 
} 

Output: 6

  • Extern – If we need to share the same variable inside two same files, then we can extern storage class. Extern variables have global scope and are accessible outside the file in which it is declared. It is visible to all programs. Initially, By default, these variables are assigned the value 0.
    Scope – The scope of extern variables is between global multiple files. It is used if two or more files are sharing the same variable or function. 
    Lifetime – The extern variables exist as long as the function(in which it is declared) is not terminated. 

Syntax:

extern data_type var_name1; 

Code:

C++ Code

#include<iostream> 
using namespace std; 
int a; 
int main() { 
extern int a; 
cout << "Value of a before initialization: " << a << endl; 
a = 5; 
cout << "Value of a after initialization: " << a << endl; 
} 

Output:

Value of a before initialization: 0
Value of a after initialization: 5

Mutable – The mutable storage class specifier is only used on a class data member to make it editable even if the member is part of a const object. The mutable specifier cannot be used with names that are static or const, or with reference members. 

Scope – The scope of the mutable variables is within the block, in which it is declared. 

Lifetime – The mutable variables exist as long as the class(in which it is declared) is not terminated. 

Special thanks to Ishita Dhiman for contributing to this article on takeUforward. If you also wish to share your knowledge with the takeUforward fam, please check out this article