Course Content
C++ Introduction
0/1
C++ Variables & Constants
0/1
C++Scope of Variable
0/1
C++ Keywords & Identifiers
0/1
C++ Data Types
0/1
C++ Basic I/O
0/1
C++ Type Conversion
0/1
C++ Operators
0/1
C++ Comments
0/1
C++ If-else
0/1
C++ Ternary Operator
0/1
C++ for Loop
0/1
C++ Ranged for Loop
0/1
C++ while/do-while Loop
0/1
C++ break Statement
0/1
C++ Continue Statement
0/1
C++ switch Statement
0/1
C++ goto Statement
0/1
C++ Functions
0/1
C++ User-defined Functions
0/1
C++ Default Arguments
0/1
C++ Storage Class
0/1
C++ Recursion
0/1
C++ Return by Reference
0/1
C++ Arrays
0/1
C++ Multi-dimentional Arrays
0/1
C++ Arrays & Function
0/1
C++ String
0/1
C++ Structure
0/1
C++ Structure & Functions
0/1
C++ Pointers to Structure
0/1
C++ Pointers
0/1
C++ Void Pointers
0/1
C++ Pointers & Arrays
0/1
C++ Pointers & Functions
0/1
C++ Dynamic Memory Allocation
0/1
C++ OOPs Concepts
0/1
C++ Objects and Class
0/1
C++ Constructors
0/1
C++ Destructors
0/1
C++ Constructor Overloading
0/1
C++ Objects & Function
0/1
C++ Enumeration
0/1
C++ Inheritance
0/1
C++ Inheritance Access Control
0/1
C++ Inheritance Types
0/1
C++ Polymorphism
0/1
C++ Function Overloading
0/1
C++ Function Overriding
0/1
C++ Operator Overloading
0/1
C++ Friend Function
0/1
C++ Virtual Function
0/1
C++ Abstract Class & Pure Virtual Function
0/1
C++ Encapsulation
0/1
C++ Abstraction
0/1
C++ Templates
0/1
C++ Exception Handling
0/1
C++ Multithreading
0/1
C++ Standard Library
0/1
C++ Programming Tutorials
About Lesson

C++ Storage Classes

In this tutorial, you will learn about different storage classes in C++. Namely: auto, register, static, extern and mutable with the help of examples.


Storage Class

Storage Classes are used to describe the features of a variable/function. These features basically include the scope, visibility and life-time which helps us to trace the existence of a particular variable during the runtime of a program.

Every variable in C++ has two features: type and storage class. Type specifies the type of data that can be stored in a variable. For example: intfloatchar etc.

Storage classes are used to specify the lifetime (determines how long a variable can exist) and scope of variable (determines which part of the program can access it) or function within a program. The storage specifiers is like how storage is allocated and how variable is treated compiler depends on the storage classes.


Syntax

To specify the storage class for a variable, the following syntax is to be followed:

storage_class var_data_type var_name; 

Type of Storage Class

Depending upon the storage class, it can be divided into 5 types:

  1. Auto
  2. register
  3. static
  4. extern
  5. mutable

1. Auto Storage Class

  • The auto keyword provides type inference capabilities, using which automatic deduction of the data type of an expression in a programming language can be done.
  • The auto storage specifier is the default storage specifier for all local variables.
  • Its scope is only limited to the function where it is defined. In simple terms, local variable exists and can be accessed only inside a function.
  • The life of a local variable ends (It is destroyed) when the function exits.
  • These variavle are also called as Local variable defined inside a function (defined inside function body between braces).

Example 1: Auto variable

// This program to check if you are 18 or not using goto statement.

# include <iostream>
using namespace std;
void num();

int main() 
{
    // local variable to main()
    int n = 5;
    num();

    // illegal: n1 not declared inside main()
    n1 = 9;
}
void num(){
    // local variable to num()
    int n1;
    n1 = 6;
                                  
    // illegal: n is not declared inside num()
    cout << n;
}

The variable n cannot be used inside num() and n1 cannot be used inside main() function.

Keyword auto was also used for defining local variables before as: auto int n;

But, after C++11 auto has a different meaning and should not be used for defining local variables.


Example 2: Auto Variable

#include <iostream>
using namespace std;

void autoStorageClass(){
    cout << "Demonstrating auto class\n";
    
    // declarationof an auto variable
    // no data-type declaration needed
    auto a = 2;
    auto b = 2.2;
    auto c = "AlgBly";
    
    // display the auto variable
    cout << a << "\n";
    cout << b << "\n";
    cout << c << "\n";
}                        
int main(){

    // to Demonstrating auto storage class
    autoStorageClass();

    return 0;
}

Output 1

Demonstrating auto class
2
2.2
AlgBly

2. Register Storage Class

  • This storage class declares register variables which have the same functionality as that of the auto variables.
  • The register variable allocated memory in register than RAM. This makes it faster than the local variables.
  • Register variables are similar to automatic variables and exists inside a particular function only. It is supposed to be faster than the local variables.
  • A register variable can be declared only within a block, which means we cannot have global or static register variables.
  • It is basically used to tell the compiler to make access to the variable as fast as possible. It increases the access speed.
  • However, this keyword was deprecated in C++11 and should not be used.

Example 3: Auto Variable

#include <iostream>
using namespace std;

void registerStorageClass(){    

    cout << "Demonstrating of register variable\n";
    // declaration of an register variable
    register int a = 2;
    register char b = 'A';
    
    // display the register variable
    cout << "Value of the variable 'a' : " << a  << endl;
    cout << "Value of the variable 'b' : " << b;
}                        
int main(){

    // to Demonstrating register storage class
    registerStorageClass();

    return 0;
}

Output

Demonstrating auto class
Value of the variable 'a' : 2
Value of the variable 'b' : A

3. Static Storage Class

  • The static storage class is used for specifying static variables.
  • Static variables preserve their value (i.e. the last value) even when they are out of their scope.
  • static variables are initialized and allocated memory only once at the begining of the program.
  • The static variable retain its value until the end of the program.
  • Global static variables can be accessed anywhere in the program. By default, they are assigned the value 0 by the compiler.

For example:

... .. ...
int main()
{
    static float a;
    ... .. ...
}

A static local variable exists only inside a function where it is declared (similar to a local variable) but its lifetime starts when the function is called and ends only when the program ends.

The main difference between local variable and static variable is that, the value of static variable persists the end of the program.


Example 4: Auto Variable

#include <iostream>
using namespace std;

// Function containing non-static variables
// memory retained during execution                               
int staticFunc(){
    // num is a static variable
    cout << "For Static Variables : ";
    static int num = 0;
    num++;
    return num;                                 
}
// Function containing non-static variables
// memory destroyed
int nonStaticFunc(){
    // num is a static variable
    cout << "For Non-Static Variables : ";
    int num = 0;
    num++;
    return num;                                 
}

int main()
{
    // Calling the static variable
    cout << staticFunc << endl;
    cout << staticFunc << endl;
    // Calling the Non static variable
    cout << NonStaticFunc << endl;
    cout << NonStaticFunc << endl;

    return 0;
}

Output

Static variables: 1
Static variables: 2
Non-Static variables: 1
Non-Static variables: 1

working of ablve program

  • In the above program, num() function is invoked 4 times. First 2 time as static and last 2 time as non-static variable.
  • During the first call, variable num is declared as static variable and initialized to 0. Then 1 is added to num which is displayed in the screen.
  • When the function num() returns, variable num still exists because it is a static variable.
  • During second function call, no new variable num is created. The same num is increased by 1 and then displayed to the screen.

Output of above program if num was not specified as static variable

Non-Static variables: 1
Non-Static variables: 1

4. Extern Storage Class

  • The extern storage specifie is used to access the variable from which is declared and defined in other file.
  • The extern storage class id required when th evariable need to be shared across the multiple files.
  • If a variable is defined outside all functions, then it is called a global variable.
  • It is visible to all the programs. It is used if two or more files are sharing the same variable of function.
  • The scope of a global variable is the whole program. This means, It can be used and changed at any part of the program after its declaration.
  • The lifetime of the extern variable is as long as the program in which it is declared is terminated.

Example 5: Extern Storage class

#include <iostream>
using namespace std;

// Global variable declaration
int a;

void externStorageClass(){
    cout << "Demonstrating extern Class : " << endl;
    extern int a;
    cout << "The value of 'a' : " << a << endl;
    // Value of extern variable a modified
    a = 2;
    
    // display the modified value
    cout << "Modified value of 'a' : " << a;
}
                                  
int main(){
                                  
    // externStorageClass
    externStorageClass();
    return 0;
}

Output

Demonstrating extern Class : 
The value of 'a' : 0
Modified value of 'a' : 2

In the above program, a is a global variable.

This variable is visible to both functions main() and externStorageClass() in the above program.


5. Mutable Storage Class

  • The mutable storage specifier is applied only to class objects.
  • The keyword mutable is mainly used to allow a particular data member of const object to be modified.
  • It allows a member of an object to override the const member function.
  • A mutable member can be modified by a const member function.

Example 6: Mutable Storage class

#include <iostream>
using namespace std;

class mutableStorageClass{
    public : mutableStorageClass(int a, int b){
        x = a;
        y = b;
    }

    int  x;
    mutable int y;
};
int main(){
    const mutableStorageClass temp(4, 2); // x = 4 and y = 2

    cout << "Demonstrating mutable Class : " << endl;
    cout << "x : " << temp.x << ", y : " << temp.y << endl;
    temp.y = 3;  // Correct value of 'y' b'cos it is mutable

    // display the modified value
    cout << "x : " << temp.x << ", y : " << temp.y << endl;

    return 0;
}

Output

Demonstrating mutable Class : 
x : 4, y : 2
x : 4, y : 3
Exercise Files
No Attachment Found
No Attachment Found
error: Content is protected !!