Grasping C / C++ Storage Classes in Just 3 Minutes

“Unlock the Secrets of C/C++ 4 Storage Classes: – Auto, Register, Static, and Extern. Explore Their Meanings, Use Cases, with Examples.

In C/ C++, storage classes are used to define the scope, lifetime, and visibility of variables and functions within a program.

There are four storage classes in C++: auto, register, static, and extern. Here are the key points and examples for each of them:

Auto:

  • Auto is the default storage class for local variables.
  • It is rarely used explicitly since local variables are automatically considered “auto.”

Example:

int main() {
  auto x = 7; 
  return 0;
}

NOTE: Generally we don’t use the auto in the program as they are by default for local variables. Now in C++, the “auto” keyword is being used for type deduction.

Register:

  • The register storage class is used to define local variables that should be stored in a register instead of memory.
  • The compiler ultimately decides whether to store the variable in a register or memory.
  • It is often used when quick access to a variable is required.

Example:

int main() {
  register int count = 0; // 'register' storage class is used here
  return 0;
}

Static:

  • Static storage class is used to define variables that retain their values even after the scope in which they are defined has ended.
  • It can be used for both local and global variables.
  • Local static variables are initialized only once, and their values persist across multiple function calls.

Example:

void foo() {
  static int count = 0; // 'static' storage class is used here
  count++;
  cout << "Count: " << count << endl;
}

int main() {
  foo(); // Output: Count: 1
  foo(); // Output: Count: 2
  return 0;
}

Extern:

  • The extern storage class is used to declare a global variable or function that is defined in another file.
  • It is used when you want to access a variable or function from multiple files.
  • The variable or function must be defined in another file for it to work correctly.

Example:

// File: file1.cpp
extern int x; // 'extern' storage class is used here to declare a variable defined in another file

// File: file2.cpp
int x = 5; // Definition of the variable 'x'

// File: main.cpp
#include <iostream>
extern int x; // 'extern' storage class is used here to access the variable 'x' defined in another file

int main() {
  std::cout << "x: " << x << std::endl; // Output: x: 5
  return 0;
}

Important points about storage classes:

  • The auto storage class is rarely used explicitly in modern C++ since it is the default.
  • The register storage class does not guarantee that a variable will be stored in a register; it is a hint to the compiler.
  • Static variables inside functions are often used for maintaining state across function calls.
  • External variables or functions declared with the extern storage class must be defined in another file.

SIDE NOTE: Is MUTABLE a storage class in C++?

Many people think that the “mutable” is also a storage class in C++. But the answer is NO.

mutable is not a storage class in C++. It is a keyword used to modify the behavior of class members, particularly data members of a class. When a data member is declared as mutable, it can be modified even within a const member function of the class. This allows for changing the state of the object without affecting the const-ness of the function.

For example:

class Example {
public:
    mutable int mutableValue;  // This data member can be modified within a const member function
    int regularValue;          // This data member can't be modified within a const member function
    
    void ConstFunction() const {
        // mutableValue can be modified here
        // regularValue can't be modified here
    }
};

So, while mutable affects how data members can be modified within const member functions, it’s not related to the broader concept of storage classes like auto, register, static, and extern.