CodesJava

Easy learning with example program codes

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 Classes Storage Place Default Value Scope Life-time Features
auto RAM Garbage Local Within function Default storage class.
extern RAM 0 Global Till the end of main program Visible to all the programs.
static RAM 0 Local Till the end of main program Retains value between multiple functions call.
register Register Garbage Local Within function Faster access than other variables.
mutable Garbage Local Within Class Used 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
Posted in CPP   
Sign Up/ Sign In
Ask a Question


Industrial Training

We offers Placement Oriented Training on Java, Spring, JSF, Hibernate, PHP, AngularJS, Angular 4, PLSQL, Oracle BI Publisher etc. We also provides Online training, please mail us at hr@codesjava.com.

Development

We also provides the Development services for Website Development , Java Development, PHP Development, Android App Development etc. You can contact us on hr@codesjava.com.

Copyright © 2018 CodesJava DMCA.com Protection Status SiteMap Reference: Java Wiki