CPP Storage Classes

A Storage class in CPP represents the scope of a variable within the program. The storage classes in CPP are discussed below:

 

Storage ClassesStorage PlaceDefault ValueScopeLife-timeFeatures
autoRAMGarbageLocalWithin functionDefault storage class.
externRAM0GlobalTill the end of main programVisible to all the programs.
staticRAM0LocalTill the end of main programRetains value between multiple functions call.
registerRegisterGarbageLocalWithin functionFaster access than other variables.
mutableGarbageLocalWithin ClassUsed only for class data members.

 

Example:

#include <iostream.h>
using namespace std;
 
int demo()
{    
static int i=0;
register int j=0;
auto k=0;
i++;    
j++;
k++;
cout << "i = " << i << endl;    
cout << "j = " << j << endl;    
cout << "k = " << k << endl;   
return 0;
}  
int main()
{    
demo();   
demo();   
demo();  
return 0;
}

Output

i = 1
j = 1
k = 1
i = 2
j = 1
k = 1
i = 3
j = 1
k = 1
Content Protection by DMCA.com
Please Share