Storage classes in C++

  • October 1, 2022
  • C++
Accessing Elements

Storage classes in C++ are type specifiers that aid in defining the lifetime and visibility of variables and functions within a C++ program. C++ storage classes aid in determining the existence of a specific variable or function during the execution of a program.

Syntax: 

storage_class var_data_type var_name;
Types of storage classes:

Five different kinds of storage classes can be used in a C++ program.

  • Automatic
  • Register
  • Static
  • External
  • Mutable
Automatic:

In C++, the default storage class for all local variables is the automatic storage class. The auto keyword is used to declare the variable storage class. In C++, the automatic storage class can also be used for automatic data type deduction, and as such, it can be used when declaring a variable without specifying the data type.

Example:
#include <iostream>
using namespace std;

void autoStorageClass()
{

	cout << "Demonstrating auto class\n";

	// Declaring an auto variable
	// No data-type declaration needed
	auto a = 32;
	auto b = 3.2;
	auto c = "CODERZ";
	auto d = 'Z';

	// printing the auto variables
	cout << a << " \n";
	cout << b << " \n";
	cout << c << " \n";
	cout << d << " \n";
}

int main()
{

	// To demonstrate auto Storage Class
	autoStorageClass();

	return 0;
}
Output:
Demonstrating auto class
32 
3.2 
CODERZ 
Z 
Register:

The register storage class in C++ utilizes CPU registers to store data to access data quickly in a function. The functionality of register variables is similar to automatic variables. The only difference is that the compiler tries to store the variable in the register instead of the memory if the registers are free. The compiler stores the register variables in the memory if no registers are free.

Example:
#include <iostream>
using namespace std;

void registerStorageClass()
{

	cout << "Demonstrating register class\n";

	// declaring a register variable
	register char b = 'Z';

	// printing the register variable 'b'
	cout << "Value of the variable 'b'"
		<< " declared as register: " << b;
}
int main()
{

	// To demonstrate register Storage Class
	registerStorageClass();
	return 0;
}
Output:
Demonstrating register class
Value of the variable 'b' declared as register: Z
Static:

In C++, the static storage class is used to declare a variable that, once initialized, is not deallocated when it exits the scope. As a result, the variables do not lose their values within function calls because their lifetime is the entire program. It is useful in recursive calls because static variables are only initialized once within a function block and can be changed on each recursive call without being re-initialized.

Example:
#include <iostream>
using namespace std;

int staticF()
{
	cout << "For static variables: ";
	static int count = 0;
	count++;
	return count;
}

// Function containing non-static variables
// memory is destroyed
int nonStaticF()
{
	cout << "For Non-Static variables: ";

	int count = 0;
	count++;
	return count;
}

int main()
{

	// Calling the static parts
	cout << staticF() << "\n";
	cout << staticF() << "\n";
	;

	// Calling the non-static parts

	cout << nonStaticF() << "\n";
	;
	cout << nonStaticF() << "\n";
	;
	return 0;
}
Output:
For static variables: 1
For static variables: 2
For Non-Static variables: 1
For Non-Static variables: 1
External:

When a variable or function is declared on a separate function block from the definition, the external storage class is used in C++. That is, a variable or function can be defined in one code block or even a file, then declared and used in another code block or file. The extern keyword tells the compiler that the variable or function is defined somewhere else.

Example:
#include <iostream>
using namespace std;

int x;
void externStorageClass()
{

	cout << "Demonstrating extern class\n";

	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;

	cout
		<< "Modified value of the variable 'x'"
		<< " declared as extern: \n"
		<< x;
}

int main()
{

	// To demonstrate extern Storage Class
	externStorageClass();

	return 0;
}
Output:
Demonstrating extern class
Value of the variable 'x'declared, as extern: 0
Modified value of the variable 'x' declared as extern: 2
Mutable:

In C++, the mutable storage class is used to declare variables that will be changed in otherwise constant objects or classes. The mutable storage class has the same lifetime as the class and has local visibility. A mutable storage class’s initial value is a garbage value.

Example:
#include <iostream>
using std::cout;

class Test {
public:
	int x;
	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;
	// cout << t1.x;
	return 0;
}
Output:
20

Note: also read about Jump Statement in C++

Follow Me

If you like my post please follow me to read my latest post on programming and technology.

https://www.instagram.com/coderz.py/

https://www.facebook.com/coderz.py

Leave a Reply

Your email address will not be published. Required fields are marked *