The Storage class in C++ defines the scope, life-time and where to store a variable in C++ program. These specifiers precede the type that they modify. There are four storage classes defined in C++ programming language
- auto
- static
- register
- extern
auto Storage Class
A variable which is declared inside a function or block is automatic variable by default. We can declare automatic variables using auto keyword, but it is rarely used because by default every local variable is automatic variable.
For Example :int count; auto int size;Both count and size are automatic variable. C++ Local Variable(auto) Example Program
#include <iostream> using namespace std; int main(){ // Automatic variable by default int a = 5; // Declaration of automatic variables using auto keyword auto int b = 10; cout << "Sum = " << a+b; return 0; }Output
Sum = 15
static Storage Class
A local static variable is visible only inside their own function but unlike local variables, they retain their values between function calls. We can declare static variable by adding static keyword before data type in variable declaration statement.
static int count;
- Variables declared static are initialized to zero(or for pointers, NULL) by default.
- Static keyword has different effect on local and global variables.
- For local static variables, compiler allocates a permanent storage in heap like global variable, so that they can retain their values between function calls. Unlike global variables, local static variables are visible only within their function of declaration.
- For global static variables, compiler creates a global variable which is only visible within the file of declaration.
- In C++, when static is used on a class data member, it causes only one copy of that member to be shared by all objects of its class.
C++ Static Variable Example Program
In the following program, we declared a local and a static variable inside getVal function. Output of this programs shows that static variable retains it's value between successive function call whereas local variable vanishes when control exits function block.
#include <iostream> using namespace std; int printVal(){ // Declaring a loval static variable static int sVariable = 0; // Declaring a local variable int lVariable = 0; // Incrementing both variables sVariable++; lVariable++; // Static variable will retail it's value between successive function calls cout << "StaticVariable = "<<sVariable<<" LocalVariable = "<<lVariable; cout << endl; } int main(){ printVal(); printVal(); printVal(); printVal(); return 0; }Output
StaticVariable = 1 LocalVariable = 1 StaticVariable = 2 LocalVariable = 1 StaticVariable = 3 LocalVariable = 1 StaticVariable = 4 LocalVariable = 1
register Storage Class
Declaring a variable with register keyword is a hint to the compiler to store this variable in a register of the computer's CPU instead of storing it in memory. Storing any variable in CPU register, will reduce the time of performing any operation on register variable. We can declare register variables using register keyword.
For Example :register int count;
- Declaring a variable as register is a request to the compiler to store this variable in CPU register, compiler may or may not store this variable in CPU register(there is no guarantee).
- Register variable was deprecated in C++11.
- The scope of register variables are same as automatic variables, visible only within their function.
- Frequently accessed variables like loop counters are good candidates for register variable.
- We can only declare local variables and formal parameters of a function as register variables, global register variables are not allowed.
C++ Register Variable Example Program
#include <iostream> using namespace std; int main(){ // Declaration of a variables using register keyword register int counter; int sum=0; // Variable counter is used frequently within for loop for(counter = 1; counter <= 500; counter++){ sum+= counter; } cout << "Sum = " << sum; return 0; }Output
Sum = 125250
extern Storage Class
External variables in C++ are variables which can be used across multiple files. We you can declare an external variable by preceding a variable name with extern specifier. The extern specifier only tells the compiler about the name and data type of variable without allocating any storage for it. However, if you initialize that variable, then it also allocates storage for the extern variable. The extern modifier is most commonly used when there are two or more files sharing the same global variables or functions.
- Variables declared extern are initialized to zero by default.
- The scope of the extern variable is global.
- The value of the external variable exists till program termination.