C++ Storage Classes


Storage Classes describes the visibility, memory location and lifetime of variables and/or functions in Storage Classes C++. There are 5 storage classes in C program:

  • Automatic Storage Class - It’s a local method to block the which variable is defined.
  • External Storage Class - Global, available everywhere in the program.
  • Static Storage Class - It’s a local method to block the which variable is defined.
  • Register Storage Class - It’s a local method to function in which it is declared.
  • Mutable Storage Class - It's applies only to class objects.

Automatic Storage Class

The auto is the keyword for automatic variables. All local variable is default automatic.

Syntax

auto int age;
int name; //default automatic variable

Static Storage Class

A static is the keyword for static variables. It is used to refer to the global variable. It contains one copy of member to be shared by the objects of the class.

Syntax

static int age; 

Register Storage Class

The register is the keyword for register variables. It is defined as a local variable and its store in the register instead of Random Access Memory (RAM).

Syntax

register int age; 

External Storage Class

The extern is the keyword for external variables. It is used to refer to the global variable. It is available to all the program files.

Syntax

extern int age; 

Example

#include <iostream> 
using namespace std;

int main() 
{  
	static int a=14; 
    int b=9;
    register int c=3;
    int d=5;
    cout << a <<" "<< b <<" "<< c <<" "<< d << endl; 
    return 0;
}



OnlineTpoint is a website that is meant to offer basic knowledge, practice and learning materials. Though all the examples have been tested and verified, we cannot ensure the correctness or completeness of all the information on our website. All contents published on this website are subject to copyright and are owned by OnlineTpoint. By using this website, you agree that you have read and understood our Terms of Use, Cookie Policy and Privacy Policy.