Storage Classes In C++ | Storage Classes With Example

In this article, we’ll learn about storage classes present/used in C++. So, first of all we should know what is a storage class and what does it do?

Storage Class:

It is basically a class of storage which the compiler needs when storing a variable and/or function. Storage class defines the scope or what we call as visibility and lifetime of the variable. Lifetime of a variable means the time period till which that variable will be active in a program and scope means till which point the variable can be used or called or will be accessible.

We know in C there are 4 types of storage classes:

  1. Automatic
  2. Register
  3. External
  4. Static

In addition to all these, in C++ we have one more storage class:

5. Mutable

Let’s discuss more about these five storage classes.

Automatic Storage Class:

Variables specified with this storage class are local variables. In other words, they have local scope. So, its lifetime is within the function and it can be used only inside that function not outside the function. The keyword used for automatic storage is auto. If we don’t specify any storage class during declaration of the variable, the compiler treats the variable as automatic storage class implicitly by default. Hence, it is also called default storage class.

Example Code:

 #include <iostream>
using namespace std;

void autoStorageClass()
{
	cout<<"Examples of AutoStorageclass"<<endl;
	auto int a = 16;
	auto float b = 2.7;
	auto char c = 'S';

	cout << a << " \n";
	cout << b << " \n";
	cout << c << " \n";
}

int main()
{
autoStorageClass();

	return 0;
}

Output:

DwIjPGqk6jDz 0AXWfL M6rJFzGvsJF Z6OzrbRkwjR6QH4Xh3yucNiAn jWAQu1AkQaiRq3u38RXNNI8NArJm ff6Ka2BEsick2jA9OdfVUiC5mo630ndy2HoZVk5u9ypV2qQ

Register Storage Class:

So, for otherstorage classes the variable is stored in RAM but for Register storage class the variable will be assigned to register memory. But there is a disadvantage which is that the CPU will be having limited register memory. So, it depends on the availability of the register memory. If the register memory is available then the data will be stored otherwise it will be lost. The size of the variable depends on the size of the register. The keyword used for Register storage class is register.

Example Of Register Storage Class:

#include <iostream>
using namespace std;

void registerStorageClass()
{

	cout << "Register class Example\n";
	register int i = 234;

	cout << "Value of the variable i:"<< i;
}
int main()
{

	registerStorageClass();
	return 0;
}

Output:

vxvqbTpr3cjmEBalt4hvgQIP6GBVQBcYoKlbI0DgvYj1Gj7n BF25r4GJVogOTDDIfkq olhpMKx

External Storage Class:

These variables which are declared with Extern keyword can be used outside the program also. The same variable could be used for two or more files through external storage class.

Example Of External Storage:

//Example code: subfile.cpp
int sum=20;

void add(int s)
{
	sum=sum+s;
}

Output Of External Storage:

7L9amXGYzsFveHZ6zlaVB2eMSEkpxrAE7i1cZSCLWZiPLIeO TpHi0g3zv17Zs8Wu11pbLtsRWdGmIPj0drPsspYfUdCjHN4feLGIQ sstu4 IYO5kX4gIu VuCif X4gaWbw

Example Of External Storage:

Mainfile.cpp: #include<iostream>
#include "subfile.cpp"  
using namespace std;

extern int sum;  

int main()
{
    cout<<sum<<endl;
    add(20);
    cout<<sum<<endl;
    return 0;
}

Output Of External Storage:

Static Storage Class:

In static storage class, the variable has a lifetime of the entire program. The variable will not be destroyed even if the pointer has come out of the function where the variable is declared. The variable is only initialized once. So, the keyword used for this storage class is static.

Example Static Storage Class:

 #include <iostream>
using namespace std;

int staticvariables()
{
	cout << "For static variables: ";
	static int i = 5;
	i++;
	return i;
}

int main()
{
	cout<<"Displaying static variables incremented."<<endl;
	cout << staticvariables() << "\n";
	cout << staticvariables() << "\n";
	return 0;
}

Output Of Static Storage Class:

qRqhlXPvTiGNBa02njne 5Uj1tp68NLqooNi215Q P EyuVtxdQ

Mutable Storage Class:

This storage class is only used/applied to class objects. By using the Mutable storage class, we can override a constant member function with a member of an object which means the constant member function can modify a mutable member.

Example Of Mutable Storage Class:

#include <iostream>
using std::cout;

class mute {
public:int a;
mutable int b;
mute() { a = 6; b = 24; }
};
int main()
{
	const mute m1;
	m1.b = 50;
	cout << m1.b;
	return 0;
}

Output Mutable Storage Class:

NJMbj pexDZUw1LUq38kD3YcL7PrADjYqms8eqG81txg5bmArB55Pd

As you can see in the above example the output is 50 which means variable b is modified.

So, we learnt about every storage class in C++ but while using them we have to remember some data given in the table below.

Storage classKeywordInitial ValueLifetimeVisibility
AutomaticautogarbageFunction blockLocal
RegisterregistergarbageFunction blockLocal
StaticstaticzeroWhole programLocal
ExternalexternZeroWhole programGlobal
MutablemutablegarbageClassLocal

Please write comments or WhatsApp if you find anything incorrect, or you want to share more information about this topic discussed above.

Share Your Love
Default image
Shiva Patra
Hello, this is G. Shiva. Patra, your content writer for this website, is currently pursuing BCA for a graduation degree. I am here to write content based on C++ and similar kind of stuff. I like the tech world as it grows really fast and you have great things to learn every day which keep you away from boredom. So, I have some knowledge on very different topics related to tech like the dark web, deep web, onion rooting, IoT, Cybersecurity, and I always keep track of new devices and software used by them as every tech enthusiast does.
Articles: 9

Newsletter Updates

Enter your email address below to subscribe to our newsletter

Leave a Reply