Close
Close

Storage Classes in C++


Each variable has a storage class which defines the features of that variable. It tells the compiler about where to store the variable, its initial value, scope ( visibility level ) and lifetime ( global or local ).

There are four storage classes in C++.

  • auto
  • extern
  • static
  • register
  • mutable

Let's understand each of these one by one.

auto


Variables which are defined within a function or a block ( block is a section of code which is grouped together. eg. statements written within curly braces constitute a block of code ) by default belong to the auto storage class. These variables are also called local variables because these are local to the function in which these are defined. Since these variables are declared inside a function, therefore these can only be accessed inside that function. There is no need to put 'auto' while declaring these variables because these are by default auto.

Though you have been using these auto(local) variables in your entire C++ tutorial, let's see one example.

#include <iostream>

int sum(int n1, int n2){
	 auto int s;        //declaration of auto(local) variable
	 s = n1+n2;
	 return s;
}

int main(){
	int i = 2, j = 3, k;
  	k = sum(i, j);
  	std::cout << "sum is " << k << std::endl;
	return 0;
}
Output
sum is 5

Here, the variable 's' is declared inside the function 'sum' and thus can be used only inside the function. There was no need to put 'auto' while declaring it.

extern


We write extern keyword before a variable to tell the compiler that this variable is declared somewhere else. Basically, by writing extern keyword before any variable tells us that this variable is a global variable declared in some other program file.

Now let's see what actually happens.

You must be knowing what a global variable is. A global variable is a variable which is declared outside of all the functions. It can be accessed throughout the program and we can change its value anytime within any function as follows.

#include <iostream>
using namespace std;
int g;
void print(){
	 g = 10;
	 cout << "g is " << g << endl;
}
int main(){
	g = 7;
  	cout << "g is " << g << endl;
  	print();
	return 0;
}
Output
g is 7
g is 10

Here, g is the global variable defined outside of all the functions. In the main function, its value was assigned as 7 and in the print function as 10.

While declaring a global variable, some space in memory gets allocated to it like all other variables. We can assign a value to it in the same program file in which it is declared as we did in the above example. But what if we want to use it or assign it a value in any other program file.

We can do so by using the extern keyword as shown below.

firstfile.cpp

int g = 0;

In the first program file firstfile.cpp, we declared a global variable g.

Now, we will declare this variable 'g' as extern in a header file firstfile.h and then include it in a second file in which we want to use this variable.

firstfile.h

extern int g;

Now in the second program file secondfile.cpp, in order to use the global variable 'g', we need to include the header file in it by writing #include "firstfile.h". Here we assigned a value 4 to the variable 'g' and thus the value of 'g' in this program becomes 4.

secondfile.cpp

#include <iostream>
#include "firstfile.h"
int main(){
g = 4;
std::cout << g << std::endl;
return 0;
}

static

A variable declared as static once initialized, exists until the end of the program. If a static variable is declared inside a function, it remains into existence until the end of the program and not get destroyed as the function exists (as in auto). If a static variable is declared outside all the functions in a program, it can be used only in the program in which it is declared and is not visible to other program files(as in extern).

Let's see an example of a static variable.

#include <iostream>
using namespace std;
static int g = 5;
void fn(){
 	static int i = 0;
 	cout << "g = \t" << g-- << endl;
 	cout << "i = \t" << i++ << endl;
}
int main(){
	while(g >= 2)
    fn();
	return 0;
}
Output
g = 5
i = 0
g = 4
i = 1
g = 3
i = 2
g = 2
i = 3

Here, g and i are the static variables in which 'g' is a global variable and 'i' is a local variable. If we had not written static before the declaration of 'i', then every time the function 'fn()' would have been called, 'i' would have been declared every time with an initial value of 0 and as the function 'fn()' would exit, it would also have got destroyed.

register


It tells the compiler that the variable will get stored in a register instead of memory (RAM). We can access a register variable faster than a normal variable. Not all the registers defined as register will get stored in a register since it depends on various restrictions of implementation and hardware.

It is declared as follows where n is the name of an int variable:

register int roll;

mutable


We can make a class object in C++ constant using keyword const. By making the object constant, the data members of the class object cannot be modified during program execution. But sometimes we need to change some data members of a constant object. In that case, we can make the data member of a constant class object mutual so that we can change it.

Let's see an example.

#include <iostream>
using namespace std;

class square
{
	mutable int a;
	int b;
	public:
		square( int x, int y )
		{
			a = x;
			b = y;
		}
		void val() const
		{
			a = a * a;
		}
		void display() const
		{
			cout << "a : " << a << endl;
			cout << "b : " << b << endl;
		}
};

int main()
{
	const square s( 4, 7 );
	cout << "Values of a and b" << endl;
	s.display();
	s.val();
	cout << "New values of a and b" << endl;
	s.display();
	return 0;
}
Output
Values of a and b
a : 4
b : 7
New values of a and b
a : 16
b : 7

In the above example, we made the data member a of the class test mutable. A constant object s was created of the class test and the data members 'a' and 'b' were initialized to 4 and 7 respectively. The value of 'a' can be changed since we declared it mutable but the value of 'b' will remain 7 throughout the program.
We first displayed the initial values of 'a' and 'b' which are 4 and 7 respectively by calling the function display().
Then we called the function val() which squared the value of 'a' and assigned the result to 'a', thus making the value of 'a' 16.
Finally, we printed the final values of 'a' and 'b' which are 16 and 7 respectively by calling the function display().

Thus, data members declared as mutable can be modified even though they are the part of the object declared as const.

You cannot use the mutable specifier with names declared as static or const, or reference members.
To learn from simple videos, you can always look at our C++ video course on CodesDope Pro. It has over 750 practice questions and over 200 solved examples.

Ask Yours
Post Yours
Doubt? Ask question