Storage Classes In C++

In this tutorial, we will be learning storage classes in C++ with examples of all the types.

A Storage class is vital yet most of the time ignored keyword. So what is a Storage Class? Is it a place we store variables? Is it a Class having variables?

Nope. None of them. Let’s have a look at what a storage class is and its types.

Storage Classes in C++

A Storage class is a keyword in C++ used to define the life, or in coding languages, the scope of a particular variable. Adding to that some are also used to define the place the variable would be stored, i.e. RAM, Register, etc.

You might have heard of Local and Global variables. Local variable is a variable accessible only inside a function whereas global is accessible outside a particular function i.e. to many different modules.
Storage classes in a way is a continuation of this topic

There are 5 types of Storage Classes:

  1. Auto
  2. Register
  3. Static
  4. Extern
  5. Mutable

Let’s have a look at all them One By One

Auto – Storage Class in C++

If the “Auto” Keyword is used, the variable it’s used for is accessible only within that function.
Complicated? Let me explain it in simple words

Auto is nothing but a local variable.

In fact, whenever you initialize a variable, it initializes with the auto storage class.
Yup, its the default storage class
Since its the default storage class there is no compulsion to write it.

Its Syntax is:

auto <data_type> <variable>;

Here is an example to help you understand it better

#include <iostream>
using namespace std;
int main()
{
    int a;
    a = 5;
    cout<<a;
    return 0;
}

Simple Code Right? The output would be 5.
Variable ‘a’ is available only in the main function. Let’s test it. I’ll call another function and try printing ‘a’

#include <iostream>
using namespace std;
void fxn2()
{
    a = 10;
    cout<<a;
}
int main()
{
    int a;
    a = 5;
    cout<<a;
    fxn2();
    return 0;
}

Now since ‘a’ is using auto keyword, we get an error

error: ‘a’ was not declared in this scope
     a = 10;
     ^

Register – Storage class in C++

As most of you know, the Variables used in the program are stored in RAM.
RAM is the fastest method of accessing data but wait….there is a faster storage device
These are known as registers. They are capable of storing 0 or 1 only and run at least once

Variables stored using the register keyword get stored in the register instead of the RAM.

Its syntax is:

register <data_type> <variable>;

Let’s have a look at an example.

#include <iostream>
using namespace std;
int main()
{
    register int a;
    a = 5;
    cout<<a;
    return 0;
}

We’ve used the Register storage class on the variable ‘a’.
Therefore ‘a’ gets stored in registers instead of RAM

Static storage class in C++

Static is a storage keyword where the value stored in a variable is retained even after the function is over
The compiler retains this value

Syntax:

static <data_type> <variable>

Let’s have a look the example for better understanding

#include <iostream>
using namespace std;
void fxn()
{
    static int c = 5;
    c++;
    cout<<c<<" ";
}
int main()
{
    int a = 5;
    for(int i=0; i<a; i++)
    {
        fxn();
    }
    return 0;
}

Notice, we’ve used static for c. So after fxn exits, the value of c is retained. Hence the output:

6 7 8 9 10

If I remove the static variable, your output would be:

6 6 6 6 6

This is because, without the static variable, once the function exits, the value of c is deleted and reinitialized every time the function fxn is called

Extern

Ever wondered how to link two different program files?
Well, Extern is your solution

Extern takes your variable or function and makes sure it’s available in the other files.

Syntax:

extern <data_type> <variable>;
OR
extern <return_type> <function>;

This variable or function is used in another file, hence creating the link.

But a question arises, how to compile the file?
The answer is:
g++ file1.cpp file2.cpp -o write
Write means perform write operation

Here is an example of extern storage class in C++

file1.cpp:

#include <iostream>
using namespace std;
a = 123;
extern void fxn();
int main()
{
    fxn();
    return 0;
}

file2.cpp:

#include <iostream>
using namespace std;
extern int a;
void fxn()
{
    cout<<a<<" ";
    return 0;
}

And Hence the output is:

123

Mutable

Mutable keyword only applies to Classes in C++
It basically means changing the value of a constant character which is located inside a class

Syntax:

mutable <data_type> <variable>

Let me explain mutable storage class using an example

#include <iostream>
using namespace std;
class abc
{
    public:
     int a = 5;
     mutable int b = 10;
};
int main()
{
    abc obj;
    obj.b = 15;
    cout<<obj.b;
    return 0;
}

Since ‘b’ is mutable, its value could be changed. However if we had done this for a, we would have got an error

The output is:

15

And that is all. I hope you understood the logic and were able to execute it by yourself. If you have any doubts regarding this, feel free to ask it in the comment section. Thank You.

Also read:

Leave a Reply

Your email address will not be published. Required fields are marked *