top of page

Codes with Pankaj - Understanding C++ Storage Classes

Welcome to "Codes with Pankaj," your coding companion for unlocking the mysteries of C++. In this tutorial, we'll dive into the concept of storage classes, which play a crucial role in defining the scope, lifetime, and visibility of variables in your C++ programs. Whether you're a coding enthusiast or a seasoned programmer, understanding storage classes is essential for writing efficient and maintainable code.

Let's embark on this learning journey together with Pankaj!

1. Automatic Storage Class: Automatic storage class is the default storage class for local variables. Variables declared inside a function without any storage class specifier are of automatic storage class.



#include <iostream>

void automaticStorageClassExample() {
    // Automatic storage class example
    int localVar = 42;

    std::cout << "Automatic Variable: " << localVar << std::endl;
}

int main() {
    automaticStorageClassExample();

    return 0;
}
    

2. Register Storage Class: The register storage class is used to hint to the compiler that a variable will be heavily used, suggesting it be stored in a register for faster access.



#include <iostream>

void registerStorageClassExample() {
    // Register storage class example
    register int regVar = 100;

    std::cout << "Register Variable: " << regVar << std::endl;
}

int main() {
    registerStorageClassExample();

    return 0;
}
    

3. Static Storage Class: Static storage class is used to declare variables that retain their values between function calls. Explore the concept with examples demonstrating static variables.



#include <iostream>

void staticStorageClassExample() {
    // Static storage class example
    static int staticVar = 0;
    staticVar++;

    std::cout << "Static Variable: " << staticVar << std::endl;
}

int main() {
    staticStorageClassExample();
    staticStorageClassExample();
    staticStorageClassExample();

    return 0;
}
    

4. External Storage Class: The extern storage class is used to declare a variable that is defined in another file or at the top of the current file.



#include <iostream>

// External storage class example
extern int globalVar;

int main() {
    std::cout << "Global Variable (from another file): " << globalVar << std::endl;

    return 0;
}
    

5. Mutable Storage Class: The mutable specifier applies to class members of non-const objects, allowing them to be modified even in const member functions.



#include <iostream>

class MutableExample {
public:
    mutable int mutableVar;

    MutableExample() : mutableVar(0) {}

    void modifyVar() const {
        mutableVar++;
    }
};

int main() {
    MutableExample obj;
    obj.modifyVar();

    std::cout << "Mutable Variable: " << obj.mutableVar << std::endl;

    return 0;
}
    

Conclusion: Congratulations! You've explored the various storage classes in C++ with practical examples. Understanding storage classes is crucial for controlling the behavior of variables in your programs. Stay tuned for more coding adventures with Pankaj!

Related Posts

See All
bottom of page