C++ Storage Classes

    In this tutorial, we will discuss what is a storage class in C++ and how many types of Storage classes we have in C++.

    Storage Classes

    Storage classes are some predefined keywords we use in C++ they used as a prefix to the data type, and the storage class defines 3 basic properties of the variable such as variable lifetime, variable visibility and variable Initial value. Syntax to define a storage class in C++:

    <Storage_class> <data>-type> <variable_name>;

    Example:

    auto int a;

    Types of Storage Variables

    In C++ we have 5 storage classes:

    1. auto
    2. register
    3. extern
    4. static
    5. mutable
    Storage Class Specifier Keyword Storage Default Value Scope Life
    Automatic auto RAM Garbage Local (Limited to the block) Limited to the block execution
    Register register Register Garbage Local (Limited to the block) Limited to the block execution
    Static static RAM 0 Local (Limited to the block) Throughout the complete program
    External extern RAM 0 Global Throughout the complete program
    mutable mutable Ram garbage Local (Limited to the block) Class

    1. Auto

    • auto is the default storage class for all the variables if we do not define any storage class with the variable definition then it would be treated as an auto variable.
    • By default, the auto variable will hold the garbage value.
    • The scope of auto variables if limited to the function itself which means they treated as a local variable.
    • The life of the auto variable is also limited to its function block.

    Example

    #include <iostream>
    using namespace std;
    
    int main()
        {
             auto int a= 32;
             int b= 32;             // b is equal to a
             cout<<"Value of a is: "<<a<<endl;
             cout<<"Value of b is: "<<b<<endl;
        }

    Output

    Value of a is: 32
    Value of b is: 32

    2. Register

    • The register storage class is similar to auto and both shear the same functionality.
    • The only difference between register and auto is, the compiler saves the register storage class variable at free register memory instead of RAM.
    • A register is faster than RAM, because they are very close to the Operating system, so the variable performance increase when they stored at the register.
    • If there is no free register to store the variable so the variable would be stored at memory only.
    • We use a register storage class variable only when we require some variable frequently.
    • As the register does not store any memory address so using a pointer we cannot access the memory address of the register variable.
    • If we define register as the class storage of a variable, we can say that the variable is local and its lifetime is limited to its function block.

    Example

    #include <iostream>
    using namespace std;
    
    int main()
        {
             register int a= 32; // a is local variable, and its life is limited to its funciton block only
             cout<<"Value of a is: "<<a<<endl;
        }

    Output

    Value of a is: 32

    3. Static

    • static class storage used very often in C++ programming language when we require a variable in which life remains throughout the program, which means if its function gets terminated the variable and its value still remain in the memory.
    • static variables are by default local by nature which means they will present in the memory but we can not access them outside its function block.
    • A static variable does not contain any garbage value, for numeric data types its default value is 0 and for characters, it’s white space.
    • To make a static variable global we just need to define it outside a block.

    Example

    #include <iostream>
    using namespace std;
    static int b=100;  //static b is a global variable
    int main()
        {
            static int a; //here we have not assigned any value to a but static will make a 0
            cout<<"Value of a is: "<<a<<endl;
            cout<<"Value of b is: "<<b;
        }

    Output

    Value of a is: 0
    Value of b is: 100

    4. Extern

    • The extern storage class used to tell that the variable has been already defined somewhere.
    • extern often used when there is more than one C++ file, the variable is defined in one file and we want to import and used it in another file.
    • We only use extern storage class on those variables which are global and already defined somewhere.
    • It makes the variable global, so all the functions of the program can access that variable.
    • It is similar to static storage class and its variable life remains throughout the program.

    Example

    #include <iostream>
    using namespace std;
    int a=100;  //a is global varibale
    int main()
        {
            extern int a; //here we have extern "a"  which is alredy defined as global variable
            a= 20;
            cout<<"Value of a is: "<<a<<endl;
        }

    Output

    Value of a is: 20

    5. Mutable

    • With a mutable storage class, we can modify the data members of Class data structures.
    • Mutable storage class is used with class and objects (Object-oriented programming).
    • The main objective of using mutable, so we can modify the data member of the class.

    Example

    #include <iostream>
    using namespace std;
    class Hello {
    public:
        int a;
        mutable int b;
        Hello()              // Constructor
         {
             a= 20;
             b=100;
         }
    };
    int main()
    {
        const Hello obj;
        obj.b = 200; // here we have modified the value of a which is a memebr of calss Hello
        cout << obj.b;
        // obj.b = 40 would be an inappropriate operation because b is not mutable
       return 0;
    }
    

    Output

    200

    Summary

    • Storage class used as the prefix of the data types which defines the three basic properties of the variable, which are variable scope, variable lifetime, and variable initial value.
    • There are 5 types of storage class in C++, auto, extern, static, register and mutable.
    • Out of these 5 storage classes, only mutable is a direct part of Object-oriented programming.
    • By default, all the variables are auto storage class by nature.

    People are also reading: