info@tutsfinder.in



Storage Class


Storage class defines the scope, life-span and initial value of the variable and functions, it also describes the features of variables and functions.

Syntax to specify storage class:

storage_class data_type var_name; 


There are 5 storage classes in C++

1. auto

2. register

3. extern

4. static

5. mutable




1. Automatic - auto storage class lets you explicitly declare a variable with automatic storage.

It is default for variables that are described inside the block i.e. local variables.

{
   auto a=20;
}
 

2. Register – It indicates to the compiler that the object should be stored in machine register.

An object having register storage class must be defined within a block or declared as a parameter to a function.

{
   register int  month;
}

3. Extern – extern storage class objects can be used by several source files.

An extern declaration can define outside a function or at the beginning of a block.

int i = 42; // declaration and definition

extern int i;  // declaration only. 

4. Static – This storage class have static storage duration which means memory for these objects allocated when program begins run and is free when program terminates.

Static int x=5

5. Mutable – This keyword is used to modify the value of particular data member of const object.

You cannot use the mutable keyword with names declared as static or const.

mutable int x;