Storage Classes in C and C++ | Explained with Example

Storage Classes in C and C++ | Explained with Example

Every datatype has storage classes in C and C++. This class is used to define the scope and visibility of the variables.
The scope is the area of the program where the variable exists and contains a valid value. Visibility refers to the area of the code where the variable can be accessed.

By default, all the variables have ‘auto’ class. Here auto stands for automatic.

Storage Classes in C and C++ :

The various storage classes in C and C++ are –

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

C++ has another storage class apart from the above mentioned four called mutable.

1. Auto

This is the default class for all variables. The variables of this class are usual local variables.

For e.g.: The variables that we define within a particular function that cannot be accessed outside it belong to the auto class.

Basically, all variables that have the scope and are visible within a particular block of code (or within particular parenthesis) belong to the auto category storage class.

Since it is the default, the declarations –

int a;
auto int a;

Means, both of the declaration is the same.

2. Static

This is a class of variables that are visible within a particular block of code but persist throughout the program, i.e., they have a lifetime of the entire program.

They are declared as-

static int a;

These are specifically used within classes in C++ so that multiple objects can share the same variable.

If we have to count the number of objects created, we will initialize as –
static int count;

By default, the static values are initialized to zero. typedef unsigned char BYTE;

Now, since it is shared by all the objects, it won’t be re-initialized to zero after creating a new object. Instead, it will persist its value. Hence, we can record the count of the students.

3. Register

The variables that we create, initialize and use in our program are stored in RAM.

Why should you use Register variable? What are the advantages of register variable?

Every time we need that variable, its value is supposed to be fetched from RAM. This process is time-consuming in terms of CPU cycles. Hence, if a variable is to be accessed more often, we can store it in a register instead. This is mostly used for counters.
The declaration is –

register int c;

Important Points about Register variable:

  • This may (and not will) store the variable in the register as per the CPU’s convenience and quickly access it when needed.
  • The size of the variable depends on the size of the register where it is stored.
  • One important thing to remember is that we cannot use & (address of) operator for register variables. This is because there is no memory address for the registers.

4. Extern

Here, extern stands for external. This is used to re-use the globally defined variables or functions visible in all program files.

Extern keyword is used in the program that uses the globally defined variable (pre-defined in another program) in our program. Hence, we do not initialize variables with the extern keyword.

For Example:

In Program 1 :
int num; //global
void main()
{
num=5;
}

In program 2 :

extern int a //This means we are willing to re-use predefined int num in the current program.
void print()
{
cout<<a;
}

Storage Classes in C++

All the above 4 are storage classes in C and C++. Following is storage class that supports only in C++.

5. Mutable

It is used only in C++.

This keyword is used to modify the data members that should otherwise be constant or aren’t allowed to be modified.

This keyword can be used only with the data members of a class.

Now suppose we want a variable to be modified by a constant function (that cannot change any values), which generally isn’t possible, we make the variable mutable to permit this.

Similarly, if a constant object is created, none of its data members can be modified, but if any of its data members is mutable, it is an exceptional case and that data member can be modified further.

Syntax:

mutable int a;

Next…

If you want to be the expert in C and C++  programming, challenge yourself with these C and C++ coding practices.

In C, typedef is also considered as a storage class. However, its purpose is to give an alias to a particular type, or in other words, define a new kind of type.

Also, remember that one variable can have only one storage class. Multiple storage class specifiers for one variable would give a compiler error.

Leave a Reply

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