C++ Storage Classes
In this tutorial, you will learn about different storage classes in C++. Namely: auto, register, static, extern and mutable with the help of examples.
Storage Class
Storage Classes are used to describe the features of a variable/function. These features basically include the scope, visibility and life-time which helps us to trace the existence of a particular variable during the runtime of a program.
Every variable in C++ has two features: type and storage class. Type specifies the type of data that can be stored in a variable. For example: int
, float
, char
etc.
Storage classes are used to specify the lifetime (determines how long a variable can exist) and scope of variable (determines which part of the program can access it) or function within a program. The storage specifiers is like how storage is allocated and how variable is treated compiler depends on the storage classes.
Syntax
To specify the storage class for a variable, the following syntax is to be followed:
storage_class var_data_type var_name;
Type of Storage Class
Depending upon the storage class, it can be divided into 5 types:
- Auto
- register
- static
- extern
- mutable
1. Auto Storage Class
- The auto keyword provides type inference capabilities, using which automatic deduction of the data type of an expression in a programming language can be done.
- The auto storage specifier is the default storage specifier for all local variables.
- Its scope is only limited to the function where it is defined. In simple terms, local variable exists and can be accessed only inside a function.
- The life of a local variable ends (It is destroyed) when the function exits.
- These variavle are also called as Local variable defined inside a function (defined inside function body between braces).
Example 1: Auto variable
// This program to check if you are 18 or not using goto statement.
# include <iostream>
using namespace std;
void num();
int main()
{
// local variable to main()
int n = 5;
num();
// illegal: n1 not declared inside main()
n1 = 9;
}
void num(){
// local variable to num()
int n1;
n1 = 6;
// illegal: n is not declared inside num()
cout << n;
}
The variable n cannot be used inside num()
and n1 cannot be used inside main()
function.
Keyword auto
was also used for defining local variables before as: auto int n;
But, after C++11 auto
has a different meaning and should not be used for defining local variables.
Example 2: Auto Variable
#include <iostream>
using namespace std;
void autoStorageClass(){
cout << "Demonstrating auto class\n";
// declarationof an auto variable
// no data-type declaration needed
auto a = 2;
auto b = 2.2;
auto c = "AlgBly";
// display the auto variable
cout << a << "\n";
cout << b << "\n";
cout << c << "\n";
}
int main(){
// to Demonstrating auto storage class
autoStorageClass();
return 0;
}
Output 1
Demonstrating auto class 2 2.2 AlgBly
2. Register Storage Class
- This storage class declares register variables which have the same functionality as that of the auto variables.
- The register variable allocated memory in register than RAM. This makes it faster than the local variables.
- Register variables are similar to automatic variables and exists inside a particular function only. It is supposed to be faster than the local variables.
- A register variable can be declared only within a block, which means we cannot have global or static register variables.
- It is basically used to tell the compiler to make access to the variable as fast as possible. It increases the access speed.
- However, this keyword was deprecated in C++11 and should not be used.
Example 3: Auto Variable
#include <iostream>
using namespace std;
void registerStorageClass(){
cout << "Demonstrating of register variable\n";
// declaration of an register variable
register int a = 2;
register char b = 'A';
// display the register variable
cout << "Value of the variable 'a' : " << a << endl;
cout << "Value of the variable 'b' : " << b;
}
int main(){
// to Demonstrating register storage class
registerStorageClass();
return 0;
}
Output
Demonstrating auto class Value of the variable 'a' : 2 Value of the variable 'b' : A
3. Static Storage Class
- The static storage class is used for specifying static variables.
- Static variables preserve their value (i.e. the last value) even when they are out of their scope.
- static variables are initialized and allocated memory only once at the begining of the program.
- The static variable retain its value until the end of the program.
- Global static variables can be accessed anywhere in the program. By default, they are assigned the value 0 by the compiler.
For example:
... .. ... int main() { static float a; ... .. ... }
A static local variable exists only inside a function where it is declared (similar to a local variable) but its lifetime starts when the function is called and ends only when the program ends.
The main difference between local variable and static variable is that, the value of static variable persists the end of the program.
Example 4: Auto Variable
#include <iostream>
using namespace std;
// Function containing non-static variables
// memory retained during execution
int staticFunc(){
// num is a static variable
cout << "For Static Variables : ";
static int num = 0;
num++;
return num;
}
// Function containing non-static variables
// memory destroyed
int nonStaticFunc(){
// num is a static variable
cout << "For Non-Static Variables : ";
int num = 0;
num++;
return num;
}
int main()
{
// Calling the static variable
cout << staticFunc << endl;
cout << staticFunc << endl;
// Calling the Non static variable
cout << NonStaticFunc << endl;
cout << NonStaticFunc << endl;
return 0;
}
Output
Static variables: 1 Static variables: 2 Non-Static variables: 1 Non-Static variables: 1
working of ablve program
- In the above program,
num()
function is invoked 4 times. First 2 time as static and last 2 time as non-static variable. - During the first call, variable num is declared as static variable and initialized to 0. Then 1 is added to num which is displayed in the screen.
- When the function num() returns, variable num still exists because it is a static variable.
- During second function call, no new variable num is created. The same num is increased by 1 and then displayed to the screen.
Output of above program if num
was not specified as static variable
Non-Static variables: 1 Non-Static variables: 1
4. Extern Storage Class
- The extern storage specifie is used to access the variable from which is declared and defined in other file.
- The extern storage class id required when th evariable need to be shared across the multiple files.
- If a variable is defined outside all functions, then it is called a global variable.
- It is visible to all the programs. It is used if two or more files are sharing the same variable of function.
- The scope of a global variable is the whole program. This means, It can be used and changed at any part of the program after its declaration.
- The lifetime of the extern variable is as long as the program in which it is declared is terminated.
Example 5: Extern Storage class
#include <iostream>
using namespace std;
// Global variable declaration
int a;
void externStorageClass(){
cout << "Demonstrating extern Class : " << endl;
extern int a;
cout << "The value of 'a' : " << a << endl;
// Value of extern variable a modified
a = 2;
// display the modified value
cout << "Modified value of 'a' : " << a;
}
int main(){
// externStorageClass
externStorageClass();
return 0;
}
Output
Demonstrating extern Class : The value of 'a' : 0 Modified value of 'a' : 2
In the above program, a is a global variable.
This variable is visible to both functions main()
and externStorageClass()
in the above program.
5. Mutable Storage Class
- The mutable storage specifier is applied only to class objects.
- The keyword mutable is mainly used to allow a particular data member of const object to be modified.
- It allows a member of an object to override the const member function.
- A mutable member can be modified by a const member function.
Example 6: Mutable Storage class
#include <iostream>
using namespace std;
class mutableStorageClass{
public : mutableStorageClass(int a, int b){
x = a;
y = b;
}
int x;
mutable int y;
};
int main(){
const mutableStorageClass temp(4, 2); // x = 4 and y = 2
cout << "Demonstrating mutable Class : " << endl;
cout << "x : " << temp.x << ", y : " << temp.y << endl;
temp.y = 3; // Correct value of 'y' b'cos it is mutable
// display the modified value
cout << "x : " << temp.x << ", y : " << temp.y << endl;
return 0;
}
Output
Demonstrating mutable Class : x : 4, y : 2 x : 4, y : 3
Next Tutorial
We hope that this tutorial helped you develop better understanding of the concept of Storage Classes in C++.
Keep Learning : )
In the next tutorial, you'll learn about C++ Recursion
.