Storage Classes in C++


The Storage Classes in C++ define the scope or visibility and lifetime of a variable within the program. In other words, the storage class refers to the storage location of a variable. Scope or visibility refers to from which module of the program the variable is accessible. The lifetime of a variable refers to the duration till which a variable remain active during execution of the program. If the storage class is not defined for the variable, then it has some default storage values. The storage classes in C++ are of 5 types. These are:

  • auto
  • extern
  • register
  • static
  • mutable

Storage Classes in C++

These 5 storage classes in C++ are described below:

Automatic Storage Class: It is a default storage type for all the variables. These variables are declared with the keyword auto and it holds the garbage value as default value. The automatic variables are also called as Local Variables. Any variable declared within a function with auto keyword or without any keyword are always automatic variable. These variables have the visibility within the function only and it has the lifetime equal to the lifetime of the function. It has the syntax as follows:

For example:

Here, a is an automatic variable of integer type. We may assign values to these variables at the same time of the declaration as:

For example:

External Storage Class: The variables that are visible to all the functions or modules of a class are termed as Global Variables. The Globally declared variables always have the external storage class. The external variable has the visibility for all the modules or functions of the class i.e. for the whole program. Similarly, it has the lifetime equal to the lifetime of the class. The external variables are declared with the extern keyword or just outside of all the functions. It has the syntax as follows:

For example:

Here, the variable a is declared as an external variable which is of integer type. We can also assign values to these variables at the same time it is declared. For example:

Register storage class: The register variables are declared with the keyword register. These variables have the same functionality as the auto variables, but the difference is that register variables are stored in the processor registers instead of memory locations. Hence, it has the same visibility and lifetime as the auto variables i.e. it is visible within the function or module only where it is declared and it has the lifetime equal to the lifetime of the function during the program execution. The register variables are much faster than the auto variables as the variables are stored in the registers. Hence, the most frequently used variables of a program are declared as the register variable to enhance the execution speed of the program. It has the syntax as follows:

For example:

Here, the integer type variable num is declared as the register variable. We can also assign the values to these variables as:

For example:



Static storage class: The static variable is declared with the keyword static. It has the visibility as the automatic variable and lifetime equal to external variable. It means that, it has the ability of preserving its value even after coming out of its scope. Hence, after declaring the variable inside the function, it does not get destroyed even after the execution of the function and it can be used throughout the program. By default it is assigned with the value 0. It has the syntax as follows:

For example:

Here, the integer type variable no is declared as a static variable. Like the any other storage class variables, we can also add values to these variable also as:

For example:

Mutable storage class: The mutable storage class is specified with the keyword mutable. The mutable storage class is applicable to only class data members and class objects. Once a variable is declared as constant, it cannot be modified inside the class. But, using the mutable keyword it is possible to modify a constant variable and a constant class object. It has the syntax as follows:

For example:

Here, the integer type variable m is declared as the mutable variable. We can sign values to m even if it is a constant member variable as:

For example:

Close Menu