C++ Storage Classes

Storage classes in C++ indicate visibility and life-time of variables and functions.It is used to trace the existence of a variable and function during runtime.

C++ language uses 5 storage classes, namely:

Storage Classes Keyword Lifetime Visibility Initial value
Automatic auto Function Block Local Garbage
External extern Whole program Global Zero
Static static Whole program Local Zero
Register register Function Block Local Garbage
Mutable Mutable class Local Garbage

auto Storage Class

The Auto Storage class is the default storage class for all local variables declared inside a function or block.Since auto is the default storage class, this keyword is rarely used while writing c++ programs.The scope of the auto storage class is within the block. The lifetime of the auto storage class until the end of the block.

Example

int mount;
auto int month;

register Storage Class

The register storage class is the local variables that should be stored in a register instead of RAM.The register storage class has the same functionality as an auto variable.The scope of the register storage class is within block. The lifetime of the register storage class till the end of the block.

Example

register int  miles;

static Storage Class

This storage class is used to declare static variables, which have the property of preserving their value even after the scope is over.Therefore the static variable must be initialized only once and the value will be exist until the end of the program.The scope of the static storage class is within block. The lifetime of the static storage class till the end of the program.

extern Storage Class

The external storage class is used when we have a global variable that is being shared by more than one file.The value of the variable can be changed or overwritten in different blocks.The scope of the extern storage class is global multiple files. The lifetime of the extern storage class till the end of the program.

mutable Storage Class

This allows a member to override the const member function. Namely, a mutated member can be modified by a const member function.The scope of the mutable storage class is local. The lifetime of the mutable storage class within the same class.

Exercise:-

1. Which of the following is not a storage class specifier in C++?

A. auto
B. register
C. static
D. extern
E. volatile

View Answer


2. In C++, static storage class cannot be used with:

A. Global variable
B. Function parameter
C. Function name
D. Local variable

View Answer



Program :-

C++ program to create automatic, global, static and register variables.

#include<iostream>
using namespace std;
int g;    //global variable, initially holds 0
void test_function()
{
static int s;    //static variable, initially holds 0
register int r;    //register variable
r=5;
s=s+r*2;
cout<<"Inside test_function"<< endl;
cout<<"g = "<< g<< endl;
cout<<"s = "<< s<< endl;
cout<<"r = "<< r<< endl;
}
int main()
{
int a;    //automatic variable
g=25;
a=17;
test_function();
cout<<"Inside main"<< endl;
cout<<"a = "<< a<< endl;
cout<<"g = "<< g<< endl;
test_function();
return 0;
}


Output :

Inside test_function
g = 25
s = 10
r = 5
Inside main
a = 17
g = 25
Inside test_function
g = 25
s = 20
r = 5





Visit :


Discussion



* You must be logged in to add comment.