About storage classes in C++

In C++ program we use different types of storage classes to control the life time and visibility of a variable. Storage classes refers to the scope and lifetime of a variable or function.

In this guide we will discuss about various types of storage classes we use in C++. Generally we use five types of storage classes with C++ language.

Five types of storage classes are as follows;

  • Auto storage class
  • Register storage class
  • Static storage class
  • External storage class
  • Mutable storage class

Automatic storage classes

Auto storage class is the default storage class and is applied to all local variable automatically. The auto storage class works with local variable. The auto keyword is used to declare a automatic variable and the compiler will give a garbage value initially if we doesn’t assign any value to it.

Let’s see the following example where we have defined two variables with the same storage class. However, auto can use only within the function which works as a local variable.

int main(){
  auto int age;  
  int age = 23;  
}  

Register storage classes

Sometimes we use register storage class to declare a variable where we need to access quickly. The register variable allocates the memory inside the register instead of RAM. For this we can access quickly compared to other storage classes. As register class allocates memory to the register, we will not get the address of the variable declared with this storage class.

The register keyword is used to declare a variable with register storage class. The initial value of a variable declared with register variable will also be a garbage value if we don’t assign any value at the time of declaration. However, register variable is also a local variable.

Register variable is alive only inside the function or block in where they have declared. Let’s see the declaration and assigning value to a register variable.

register int x = 0;
register double profit = 123.4567;

Static storage classes

Static variable is also a local variable declared with static keyword. Once we declare a static variable, it remains live till the end of program. The initial value of a static variable is zero. They can hold their value outside of their scope.

// C++ program using static variable

#include <iostream>
using namespace std;

int static_function(){
    cout << "Value of static variable = ";
    static int counter = 0;
    counter++;
    return counter;
}

int non_static_function(){
    cout << "Value of non static variable = ";

    int counter = 0;
    counter++;
    return counter;
}

int main(){

    cout << static_function() << endl;
    cout << static_function() << endl;
    cout << static_function() << endl;

    cout << non_static_function() << endl;
    cout << non_static_function() << endl;
    cout << non_static_function() << endl;
    
    return 0;
}

Output of this static storage class program

c-storage-classes-static-variables-in-c

External storage classes

External storage class is used to declare a variable which can be used from any block or function of entire program. They are global variable and declared with extern keyword. Compiler places zero as an initial value if we don’t assign any value at the time of declaration.

External variable is not confined inside their own file rather they can be used from other file as a global variable.

// program using external storage class in c++

#include <iostream>
using namespace std;

int a;    // declare a variable which we will make external variable
void external_class(){

  extern int a;    // telling the compiler that variable a is an external variable

  cout << "Value before assigning is = " << a << endl;

  a = 5;    // modified the value of external variable

  cout << "Value after assigning is = " << a << endl;
}

int main(){

  external_class();

  return 0;
}

Output of the program:

Value before assigning is = 0
Value after assigning is = 5

Mutable storage classes

mutable keyword is used to declare a mutable variable which is active inside the class in which they are declared. When we need to modify one or more member of a class/struct then we use mutable storage class. Mutable variable works as a local variable and initialized with zero by default. They can apply only to class objects which we will discussed later.

// program using mutable storage class in c++

#include <iostream>
using std::cout;

class test_struct{
public:
	int a;

	mutable int b;    // define a mutable variable

	test_struct(){
		a = 5;
		b = 15;
	}
};

int main(){

	const test_struct test1;	// set t1 as a constant

	test1.b = 25;	// try to change the value
	cout << test1.b;

	return 0;
}

Program output:

25

Previous pageKeywords in C++

Next pageOperators in C++