Qualifiers and storage class are smaller but important programming concept that helps to make the quality of a variable more accurate for using that variable within the program. This tutorial will teach how qualifiers are used with variables and the roles of different storage classes in C++.



What is a Qualifier?

A qualifier is a token added to a variable that adds an extra quality, such as specifying volatility or constant-ness to a variable. It acts like an adjective for a variable. With or without a qualifier, the variable still holds the same amount of memory, and each bit has the same interpretation or contribution to the state/value. Qualifiers specify how it can be accessed or where it is stored. There are three qualifiers in C++. These are:

const It defines that the type is constant.
volatile It defines that the type is volatile.
mutable It applies to non-static class members of the non-reference non-const type. Mutable members of const classes are modifiable.

Storage Class in C++

Storage classes use to classify the lifetime and scope of variables. It describes how the storage for the variable is allocated and how the compiler can treat a variable; everything depends on these storage classes. Storage classes other than the data type are required to define a variable fully. Until now, you haven't used any storage class, but the default storage class was playing its role secretly. Auto is the default storage class.

These storage classes are divided into four types. They are:
  • Auto Variable
  • Register variable
  • Static Variable
  • Extern Variable

auto Variable

It is the C++ default storage class you use, or the compiler automatically assigns it to the variables. It is applied to local variables, and those variables are visible only within the function inside which it is declared. It gets terminated as soon as the function execution gets over. The variables having "auto" as their storage class contain garbage values if they are not assigned any value. The keyword used is "auto".

Example:

int var1; // by default, storage class is auto
auto int var;

register Variable

The register storage class is used to classify local variables that get stored in the CPU register instead of primary memory (RAM), which means that the variable has a maximum size equal to the register size (usually one word) and cannot have the unary '&' operator applied to it (as it does not have a memory location). The storage class register is used only for those variables that require quick access, such as counters. It is not a must that a variable declared with a register will always be stored in the CPU register; it might get stored in the CPU register, which fully depends on the hardware and implementation restriction. The keyword "register" is used for declaring register variables.

Example:

register int var2;

static Variable

The scope of a static variable is local to the function in which it is defined, but it doesn't get terminated when the function execution gets over. In other words, the static storage class tells the compiler to keep the local variable in existence during the program's lifetime instead of destroying it at the end of the function scope. In between the function calls, the value of the static variable persists. The static variable stores zero (0) as its initial value by default. The static modifier may also be applied to global variables.

Example:

static int var3 = 6;

extern Variable

Variables having an extern storage class has a global scope. Extern variables are used when programmers want a variable to be visible outside the file in which it is declared. Variables with storage class extern can be shared across multiple files. Extern variables do not end until the program execution gets terminated.

Example:

extern int var4; // declaration of variable 'var4'
int var4; // definition of variable 'var4'

Program for Static Storage Class

#include <iostream>
using namespace std; 

void fun()
{
    static int i = 6;
    i++;
    cout << i;
}

int main()
{
    fun();
    fun();
    fun();
}

Program for Register Storage Class

#include <iostream>
using namespace std; 

int main()
{
    int a,b;
    regster int c;
    cout<<"Enter the two values";
    cin>>a>>b;
    c=a+b;
    cout<<"Tot is:"<<c;
}


Found This Page Useful? Share It!
Get the Latest Tutorials and Updates
Join us on Telegram