C++ Storage Classes

Storage classes in C++ define the scope, visibility, and lifetime of variables and functions. They tell the compiler where to store a variable and how long it should exist in memory.

The four main storage classes in C++ are: auto, register, static, and extern.

1. auto Storage Class

The auto storage class is the default storage class for local variables. Variables declared inside a function are automatically auto unless specified otherwise.

  • Scope: Local to the block/function.
  • Lifetime: Exists only during function execution.
  • Memory: Stored in stack.
C++
#include <iostream>
using namespace std;

int main() {
    auto int x = 10;  // auto is optional
    cout << "Auto Variable: " << x;
    return 0;
}

2. register Storage Class

The register storage class requests the compiler to store the variable in a CPU register instead of RAM for faster access. It is generally used for frequently accessed variables like loop counters.

  • Scope: Local to the block.
  • Lifetime: Same as local variable.
  • Note: Modern compilers decide automatically whether to use registers.
C++
#include <iostream>
using namespace std;

int main() {
    register int i;
    for(i = 1; i <= 5; i++) {
        cout << i << " ";
    }
    return 0;
}

3. static Storage Class

The static storage class preserves the value of a variable between function calls. Unlike normal local variables, static variables are initialized only once and retain their value throughout program execution.

  • Scope: Local to function (if declared inside).
  • Lifetime: Entire program execution.
  • Memory: Stored in data segment.
C++
#include <iostream>
using namespace std;

void counter() {
    static int count = 0;
    count++;
    cout << "Count: " << count << endl;
}

int main() {
    counter();
    counter();
    counter();
    return 0;
}

4. extern Storage Class

The extern storage class is used to declare a global variable or function in another file. It allows sharing variables across multiple files.

  • Scope: Global.
  • Lifetime: Entire program execution.
  • Used for multi-file programs.
C++
#include <iostream>
using namespace std;

int globalVar = 50;  // Defined globally

void display();

int main() {
    display();
    return 0;
}

void display() {
    extern int globalVar;
    cout << "Extern Variable: " << globalVar;
}

5. Storage Class Comparison

Storage ClassScopeLifetimeMemory Location
autoLocalFunction executionStack
registerLocalFunction executionCPU Register (if possible)
staticLocal/GlobalEntire programData Segment
externGlobalEntire programData Segment

6. Complete Demonstration Program

C++
#include <iostream>
using namespace std;

int globalValue = 100; // For extern

void testStatic() {
    static int s = 0;
    s++;
    cout << "Static Value: " << s << endl;
}

int main() {
    auto int a = 5;
    register int i;

    cout << "Auto Variable: " << a << endl;

    for(i = 1; i <= 3; i++) {
        cout << "Register i: " << i << endl;
    }

    testStatic();
    testStatic();

    extern int globalValue;
    cout << "Extern Global Value: " << globalValue << endl;

    return 0;
}

Conclusion

Storage classes control how variables behave in memory. While auto and register are mainly used for local variables, static preserves values across function calls, and extern helps share variables across files. Understanding these concepts is essential for managing memory and scope effectively in C++.