C PROGRAMMING
MULTIPLE CHOICE QUESTION
OLD QUESTION BANK
SAMPLE QUESTION 2080 AND SOLUTION

Storage Classes are keywords used to define the scope, lifetime, and visibility of variables.Variables inside functions can have different storage classes, which determine their memory allocation, visibility, and lifespan. 

1. Automatic (auto): Variables declared within a block or function without an explicit storage class specifier default to automatic storage class. These variables are created when the block or function is entered and destroyed when it is exited. Their storage is allocated from the stack. If not initialized, they contain garbage values.

void myFunction() {
    auto int x; // Automatic variable
}

2. Register: This storage class is similar to automatic storage, but it suggests the compiler to store the variable in a CPU register for faster access. However, modern compilers are smart enough to optimize variable storage, so the use of register is mostly deprecated.

void myFunction() {
    register int x; // Register variable
}

3. Static: Variables declared with the static storage class retain their values between function calls. They are initialized only once, and their storage is allocated in the data segment of the program. They are visible only within the function where they are declared but keep their values across function calls.

void myFunction() {
    static int counter = 0; // Static variable
    counter++;
    printf("Function called %d times\n", counter);
}

4. External (extern): Variables declared with the extern storage class are defined outside of any function and can be accessed by multiple source files within the same program. They are typically used for global variables.

extern int globalVariable; // Declaration of external variable