C++ Storage Classes

In this tutorial, you will learn about different storage classes in C++ such as Automatic, External, Static, Register, Mutable with the help of examples.


In C++ Storage classes are used to describe the various features available in a function or a variable. These features include visibility, scope, and the existence of a particular variable during runtime. To specify the storage class for a particular variable, the following syntax needs to be followed of “storage_class variable_data_type variable_name”.

In C++ you can find 5 types of storage classes which are as follows:

  1. Automatic
  2. External
  3. Static
  4. Register
  5. Mutable
Storage Class Keyword Lifetime Visibility Initial Value
Automatic auto Block of function Local Garbage
External extern Full Program Global Zero
Static static Full Program Local Zero
Register register Block of function Local Garbage
Mutable mutable Class Local Garbage

Automatic

The Automatic storage class can be defined by the auto keyword which provides the basic capabilities in a normal data type of C++. This data type in C++ allows an automatic deduction of data type in C++ and is more time-efficient. All the various data types are sorted during the compiler phase only. This does increase the compilation time but makes it memory efficient. It also does not affect the runtime of any program. The following example demonstrates how to use auto variables.

#include <iostream>
using namespace std;
void autoStorageClass()
{
cout << "Demonstrating how auto class works\n";
// When declaring an auto variable
// No data-type declaration is needed
auto a = 65;
auto b = 3.7;
auto c = "TestVariable";
auto d = 'A';
// printing the auto variables
cout << a << " \n";
cout << b << " \n";
cout << c << " \n";
cout << d << " \n";
}
int main()
{
// To demonstrate an auto Storage Class
autoStorageClass();
return 0;
}

Output

Demonstrating how auto class works
65
3.7
TestVariable
A

External

The external storage class can be defined using the extern keyword. This storage class will inform the user that a specific variable is also defined somewhere else. It allows us to overwrite and change a variable in a different block or not. An extern variable is actually a global variable which is initialized with a legal value where it is declared so that it can be used elsewhere. It can be accessed from anywhere and a normal global variable can also be made external by adding the extern keyword before it. The main use of extern variables is that we can use the same variable between two different files which are part of the same program.

#include <iostream>
using namespace std;
// Declaring the variable which is to
// be made extern with an initial value and can
// also be initialized to x or not, the default value is 0
int x;
void externStorageClass()
{
cout << "Demonstrating how an extern class works\n";
// telling the compiler that the variable
// x is an extern variable and has been
// defined elsewhere (above the main
// function)
extern int x;
// printing the extern variables 'x'
cout << "Value of the variable 'x'"
<< " declared, as extern: " << x << "\n";
// value of extern variable x modified
x = 2;
// printing the modified values of
// extern variables 'x'
cout
<< "Modified value of the variable 'x'"
<< " declared as extern: "
<< x;
}
int main()
{
// To demonstrate extern Storage Class
externStorageClass();
return 0;
}

Output

Demonstrating how an extern class works
Value of the variable ‘x’ declared, as extern: 0
Modified value of the variable ‘x’ declared as extern: 2

Static

A static storage class can declare a static variable which is popularly used due to their property of preserving their value even when they are out of scope. So static variables are only initialized once and then they exist till the termination of the program. Static variables have local scope by default but global static variables can be declared and those are assigned 0 by default by the compiler.

#include <iostream>
using namespace std;
//A function containing static variables
// The memory is retained during execution
int staticFun()
{
cout << "For a Static variable: ";
static int count = 0;
count++;
return count;
}
// Function containing non-static variables
// memory is destroyed
int nonStaticFun()
{
cout << "For a Non-Static variable: ";
int count = 0;
count++;
return count;
}
int main()
{
// Calling the static parts
cout << staticFun() << "\n";
cout << staticFun() << "\n";
// Calling the non-static parts
cout << nonStaticFun() << "\n";
cout << nonStaticFun() << "\n";
return 0;
}

Output

For a Static variable: 1
For a Static variable: 2
For a Non-Static variable: 1
For a Non-Static variable: 1

Register

The Register storage class declares register variables which are very similar to the functionality of the auto variables with one difference that the variable is stored on the register of a microprocessor only if a free register is available. This makes your program much faster than other programs and should be used only when some variables need to executed frequently. One important thing to note is that one cannot obtain the address of a register variable by using pointers.

#include <iostream>
using namespace std;
void registerStorageClass()
{
cout << "Demonstrating a register storage class\n";
// declaring a register variable
register char b = 'G';
// printing the register variable 'b'
cout << "Value of the variable 'b'"
<< " declared as register: " << b;
}
int main()
{
// To demonstrate the working of a register Storage Class
registerStorageClass();
return 0;
}

Output

Demonstrating a register storage class
Value of the variable ‘b’ declared as register: G

Mutable

The mutable storage class declares mutable variables which can modify one or even more data members of any class or Structure through a constant function. The keyword mutable allows a particular data member of a constant object to be modified. When we declare any function as a constant, the ‘this’ pointer passed to the function becomes a constant. Adding the mutable to any variable allows a constant pointer to change members.

#include <iostream>
using std::cout;
class Test {
public:
int x;
// defining mutable variable y
// now this can be modified
mutable int y;
Test()
{
x = 4;
y = 10;
}
};
int main()
{
// t1 is set to constant
const Test t1;
// trying to change the value
t1.y = 20;
cout << t1.y;
// Uncommenting below lines
// will throw error
// t1.x = 8; // Because t1 is constant and the value of x cannot be changed
// cout << t1.x;
return 0;
}

Output

20

Please get connected & share!

Advertisement