Storage Classes in C++: Types of Storage Classes with Examples

Shailendra Chauhan  16 min read
13 Sep 2023

Storage Classes in C++: An Overview

Are you curious about the world of C++ and all it has to offer in C++ certification courses? Have you ever wondered how storage classes in C++ work together? In this blog post, we'll explore the ins and outs of both concepts so that you can learn more about the powerful programming language. You'll come away with an understanding of how code structures work, what storage class is all about, and a look into each of these storage classes along with their characteristics. Whether you're a beginner or an advanced user, by reading through this post, your understanding of C++ will be greatly increased!

What is Storage Class in C++ language?

Storage class in C++ language is generally used to define the visibility and lifetime of any variable or function in C++ programming including the scope of the program which will further help the programmer to understand the existence of any variable during the run time of the program. Storage class in C++ is a type of keyword that allows developers to manipulate the visibility and life cycle of program objects. Variable storage and scope are determined by storage classes, which also affect memory allocation and object accessibility.

Types of Storage Class in C++ Programming

There are 6 types of storage classes in C++ programming:

1. Automatic Storage Class in C++
2. Register Storage Class in C++
3. Static Storage Class in C++
4. Extern Storage Class in C++
5. Mutable Storage Class in C++
6. thread_local Storage Class in C++

Automatic Storage Class in C++

  • The automatic Storage Class in C++ programming provides the capabilities of type interface by automatically dedicating the data type of aj expression in any programming language.
  • This method takes less time to write the code, known by the compiler.
  • The deductions of the types generally are done in the phase of compilation but it does not affect the run time of the program.


#include <iostream>
using class std;
void autoStorageClass()
    cout << "Demonstrating auto class\n";
    // Declaring an auto variable
    // No data-type declaration needed
    auto A = 45;
    auto B = 4.5;
    auto C = "Scholarhat";
    auto D = 'S';
    // printing the auto variables
    cout << A << " \n";
    cout << B << " \n";
    cout << C << " \n";
    cout << D << " \n";
int main()
    // To demonstrate auto Storage Class
    return 0;

The auto storage class for variables is used in this C++ code. Without explicitly stating their data types, it declares and initializes variables A, B, C, and D, allowing the compiler to infer the data types on its own. The values of these auto variables are then printed.


Demonstrating auto class

Static Storage Class in C++

  • This particular static storage class in C++ programming is used for the declaration of the static variables that are generally used to write programs in C++ language.
  • Static variables have a property that helps preserve the value even after it is out of their scope.
  • So the program is initialized once and exists until the termination.
  • So no new memory allocation is not needed because the program is not redeclared.
  • The program is denied if its scope varies from local to function.
  • The global static variables can be accessed from anywhere in the program.


#include <iostream>
using class std;
// Function containing static variables
// memory is retained during execution
int staticFun()
    cout << "For static variables: ";
    static int count = 0;
    return count;

// Function containing non-static variables
// memory is destroyed
int nonStaticFun()
    cout << "For Non-Static variables: ";
    int count = 0;
    return count;
int main()
    // Calling the static parts
    cout << staticFun() << "\n";
    cout << staticFun() << "\n";

    // Calling the non-static parts
    cout << nonStaticFun() << "\n";
    cout << nonStaticFun() << "\n";
    return 0;

The behavior of static & non-static variables within functions is demonstrated by the code. While non-static variables are reset in nonStaticFun(), static variables maintain their values between calls. The program calls both functions to show the differences.


For static variables: 1
For static variables: 2
For Non-Static variables: 1
For Non-Static variables: 1

Register Storage Class in C++

  • The register storage class in C++ programming generally declares the register variables with the same functionality as the auto variables.
  • The only difference they have is that the compiler tries to store the variables in the register of the designated microprocessor if the register is available.
  • It makes the register variable work faster to store in memory during the run time of the C++ program.


#include <iostream>
using class std;
void registerStorageClass()
    cout << "Demonstrating register class\n";
    // declaring a register variable
    register char a = 'S';
    // printing the register variable 'a'
    cout << "Value of the variable 'a'"
        << " declared as register: " << a;
int main()
    // To demonstrate register Storage Class
    return 0;

The register storage class is used in this piece of C++ code for a variable called "a." However, keep in mind that because current compilers are capable of automatically optimizing variable storage and access, they frequently ignore the register keyword.


Demonstrating register class
Value of the variable 'a' declared as register: S

External Storage Class in C++

  • External storage class in C++ programming identifies the variables as defined elsewhere and not saved within the block.
  • The value that is assigned to it is in a different block and can be changed into a different block.


#include <iostream>
using class std;

// declaring the variable which is to
// be made extern an initial value can
// also be initialized to y
int y;
void externStorageClass()
    cout << "Demonstrating extern class\n";
    // telling the compiler that the variable
    // y is an extern variable and has been
    // defined elsewhere (above the main
    // function)
    extern int y;
    // printing the extern variables 'y'
    cout << "Value of the variable 'y'"
        << "declared, as extern: " << y << "\n";
    // value of extern variable y modified
    y = 3;
    // printing the modified values of
    // extern variables 'y'
        << "Modified value of the variable 'y'"
        << " declared as extern: \n"
        << y;
int main()
    // To demonstrate extern Storage Class
    return 0;

This C++ code explains how to define and access a variable with the name "y" using the extern storage class. The variable 'y' is declared as an extern, indicating that it can be used and updated inside the current scope even though it is defined elsewhere in the program (usually outside the main function).


Demonstrating extern class
Value of the variable 'y' declared, as extern: 0
Modified value of the variable 'y' declared as extern: 

Mutable Storage Class in C++

  • A mutable storage Class in C++ programming is used for modifying one or more data members of a structure through a constant function.
  • This task can easily be promoted by Mutable keywords which are particularly used to allow a particular data member of an object to be a Modifier.


#include <iostream>
using std::cout;
class Test
    int a;
    // defining mutable variable b
    // now this can be modified
    mutable int b;
        a = 4;
        b = 10;
int main()
    // t1 is set to constant
    const Test t1;
    // trying to change the value
    t1.b = 20;
    cout << t1.b;
    // Uncommenting below lines
    // will throw error
    // t1.a = 8;
    // cout << t1.a;
    return 0;

The C++ mutable keyword is used in this code to show how it works. A member variable (b) of the class may be changed even within a const object, but efforts to change a non-mutable member (a) within a const object would cause a compilation error.



thread_local Storage Class in C++

  • The new storage class introduced in C++11 is called thread_local Storage Class.
  • To define the object as thread-local, we can use the thread_local storage class specifier.
  • The attributes of the thread_local object alter in accordance with the combination of the thread_local variable with other storage specifiers, such as static or extern.


#include <iostream>
#include <thread>
// Define a thread-local variable
thread_local int threadLocalValue = 0;
// Function that uses the thread-local variable
void ThreadFunction(int threadId) {
// Modify the thread-local variable
threadLocalValue += threadId;
// Print the thread-local variable
std::cout << "Thread " << threadId << ": threadLocalValue = " << threadLocalValue << std::endl;
int main() {
// Create two threads
std::thread t1(ThreadFunction, 1);
std::thread t2(ThreadFunction, 2);
// Wait for both threads to finish
// Since threadLocalValue is thread-local, each thread has its own copy
// The main thread's threadLocalValue is still 0
std::cout << "Main Thread: threadLocalValue = " << threadLocalValue << std::endl;
return 0;

The thread_local storage class is used in this example of C++ code to make a unique copy of the variable threadLocalValue for each thread. The main thread keeps a separate copy of threadLocalValue while threads t1 and t2 edit their own copies. This demonstrates how thread-local variables preserve thread separation.


Thread 1: threadLocalValue = 1
Thread 2: threadLocalValue = 2
Main Thread: threadLocalValue = 0


1. What are storage classes with an example of Storage class in C++?

The memory allocation, scope, & lifetime of variables are described by storage classes in C and C++. Auto, static, extern, register, and mutable are a few examples.

2. What is a Storage class in C++?

A storage class in C++ establishes the scope, linkage, and duration of a variable's storage. It manages the placement and organization of variables in memory.

3. Why storage classes are used?

In order to manage memory effectively and specify variable behavior, storage classes are used to regulate the lifetime & visibility of variables in a program.

4. What are the 3 general classes of storage?

Automatic storage, such as local variables, static storage, such as global variables, and dynamic storage, such as heap-allocated variables, are the three general forms of storage.

5. How many storage classes are there in C++ and C?

There are five storage classes in C++ and C: auto, static, extern, register, & mutable.


Storage class defines the scope and lifetime of variables and/or functions within a C++ training program. In this article, we looked at how storage classes help define variables' scope and lifetime. We also saw how automatic, register, static, mutable, and extern storage classes work with examples. They allow you to control the visibility and lifetime of variables, which can be very useful in larger programs. Thanks for reading! I hope this article was helpful in understanding these two concepts.

About Author
Shailendra Chauhan (Microsoft MVP, Founder & CEO at DotNetTricks)

Shailendra Chauhan is the Founder and CEO at ScholarHat by DotNetTricks which is a brand when it comes to e-Learning. He provides training and consultation over an array of technologies like Cloud, .NET, Angular, React, Node, Microservices, Containers and Mobile Apps development. He has been awarded Microsoft MVP 8th time in a row (2016-2023). He has changed many lives with his writings and unique training programs. He has a number of most sought-after books to his name which has helped job aspirants in cracking tough interviews with ease.
Accept cookies & close this