C++ Storage Classes

In this tutorial you will learn about the C++ Storage Classes and its application with practical example.

C++ Storage Classes

In C++, Storage Classes refers to the scope or visibility and the life time of the C++ Variable. Scope of the C++ Variable defines the availability of the variable in block of the C++ Program, and by life time of variable means how long variable will persist in the program.

Functions of storage class

  • It tell the location of the C++ Variable.
  • Sets the initial or default value of C++ Variable.
  • It defines the scope of the C++ Variable.
  • It defines the life time of C++ Variable.

Types of Storage Classes

In C++, we have following four type of storage classes –

  • Automatic(auto)
  • Register(register)
  • Static(static)
  • External(extern)
Storage Class Keyword Lifetime Visibility Initial Value
Automatic auto Function Block Local Garbage
Register register Function Block Local Garbage
Static static Whole Program Local Zero
External extern Whole Program Global Zero

Automatic Storage Class

It is the default storage class for local variables. Variables with auto storage class are declared at the beginning of a code block, and memory is allocated automatically as the program execution enters to a code block and frees up automatically on exiting from the code block. The scope of automatic variables is local to the block where the declared and are not accessible directly in the other block. Variable with automatic storage class can be declared using the auto keyword as following –

Syntax:

Example :

Register Storage Class

In C++, variables with register storage class are same as local variables to the code block but the are stored in CPU register instead of computer memory. Hence it enables the quick access of that variable. Maximum size of the variable is equal to register size and dependent upon the register size. Variable with register storage class can be declared using the register keyword as following –

Recommended:-  C++ do while Loop

Syntax:

Example:

Static Storage Class

It is default storage class for global variables.Static storage class can be used only if we want the value of a variable to persist between different function calls. In C++, variable declared with static storage class will keep its value retained for different function calls. Variable with static storage class can be declared using the static keyword as following –

Syntax:

Example:

Example Program:-

When we run the above C++ program, we will see the following output –

Output:-

External Storage Class

In C++, variable declared with extern storage class is said to be “Global Variables”, it means variables is accessible throughout the program till the end of program execution. External variables are declared outside the functions and can be invoked from and anywhere in a program. External variables can be accessed very fast as compared to any other storage classes. Variable with external storage class can be declared using the extern keyword as following –

Recommended:-  C++ Data Types

Syntax:

Example:

In this tutorial we have learn about the C++ Storage Classes and its application with practical example. I hope you will like this tutorial.