Storage Classes in C++

In the world of C++, managing memory efficiently is a fundamental aspect of writing robust and efficient code. C++ provides a variety of tools and mechanisms to control how memory is allocated and deallocated, and one of the key components of this system is storage classes. Storage classes in C++ are used to define the scope, visibility, and lifetime of variables.

In the below PDF we discuss about Storage Classes in detail in simple language, Hope this will help in better understanding.

There are four primary storage classes in C++:

1.  Auto Storage Class:

  • The auto storage class is the default for all local variables in C++. If you don’t specify a storage class when declaring a variable, it is assumed to be auto.
  • Variables with auto storage class are stored in the stack memory.
    They have a limited scope, typically within the function or block where they are declared.
  • These variables have automatic storage duration, meaning they are created when the program enters the block where they are defined and destroyed when the block exits.
void someFunction() {
auto int localVar = 10;
// localVar is an auto variable

2. Register Storage Class:

  • The register storage class is used to suggest to the compiler that a variable should be stored in a CPU register for faster access.
  • Register variables are limited in number and should be used for frequently accessed variables that need to be processed quickly.
  • The use of register is just a suggestion to the compiler; it may or may not actually store the variable in a register.
register int count = 0;
// count is a register variable

3. Static Storage Class:

  • Variables with the static storage class have a longer lifetime compared to auto variables.
  • They retain their values between function calls.
  • static variables are initialized only once, at the start of the program execution, and keep their values until the program terminates.
  • They are stored in a different part of memory compared to the stack memory, usually in the data segment.
void someFunction() {
static int staticVar = 0;
// staticVar retains its value between function calls

4. Extern Storage Class:

The extern storage class is used when you want to declare a variable that is defined in another file.
It is often used when you have multiple source files in a project, and you want to share a variable among them.
The actual storage for an extern variable is defined in another source file, and you simply declare it in the file where you intend to use it.

// In one.cpp
int globalVar = 42;

// In another.cpp
extern int globalVar; // globalVar is declared, and its storage is defined in another file

Related Question

Storage classes in C++ are keywords that specify the storage duration and visibility of variables.

C++ has four primary storage classes: auto, register, static, and extern.


The ‘auto’ storage class is the default storage class for local variables. Variables declared with ‘auto’ have a block scope and automatic storage duration.

The ‘register’ storage class is used to suggest that a variable should be stored in a CPU register for fast access. However, the actual placement in a register is up to the compiler.

The ‘static’ storage class is used to declare variables with static storage duration. These variables retain their values between function calls and have file scope if declared outside functions.


Preprocessors in C++ A preprocessor

Standard Template Library in C++

Exception Handling in C++ Exception

Destructors in C++ A destructor

Constructors in C++ A constructor

Inheritance in C++ Inheritance is

C++ Classes and Objects C++

Leave a Comment

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

// Sticky ads