Storage Classes in C: auto, static, extern, register Quiz Quiz

Enhance your grasp of C programming with this quiz focusing on storage classes such as auto, static, extern, and register. Assess your understanding of variable scope, lifetime, and storage duration in C with practical examples and key distinctions.

  1. Scope of auto variables

    What is the scope of a variable declared with the 'auto' storage class in a standard C function, as in 'auto int counter;' within a function?

    1. It is limited to the block or function where it is declared
    2. It persists across the entire program
    3. It spans only one loop iteration
    4. It is globally accessible from any file

    Explanation: An 'auto' variable has block scope, meaning it can only be accessed within the function or block where it is defined. It does not persist beyond that scope, nor is it available globally across files. Variables declared as 'auto' do not last longer than a loop iteration alone, as their scope relates to blocks, not loops specifically. Thus, only block- or function-scoped is correct.

  2. Initialization behavior of static variables

    When a static int variable is declared inside a function without explicit initialization, what is its default value?

    1. 1
    2. Undefined or garbage value
    3. NULL
    4. 0

    Explanation: In C, static variables inside functions are automatically initialized to zero if not explicitly set to another value. This differs from auto variables, which would contain an undefined or garbage value if not initialized. The value one or NULL is not assigned by default to static integers.

  3. Purpose of the extern keyword

    What is the main purpose of using the 'extern' storage class when declaring a variable like 'extern int sharedValue;'?

    1. To make the variable accessible only within the current block
    2. To initialize the variable automatically to zero
    3. To declare an external variable defined in another file
    4. To store the variable in a CPU register

    Explanation: The 'extern' keyword tells the compiler that the variable’s definition exists elsewhere, often in another file, allowing cross-file variable sharing. It does not store a variable in a CPU register—that relates to the 'register' keyword. ‘Extern’ has no default initialization behavior and does not restrict scope to blocks.

  4. Usage limitation of register storage class

    Which of the following is NOT permitted with a variable declared using the 'register' storage class, such as 'register int i;' inside a loop?

    1. Taking the address of the variable using the u0026 operator
    2. Using the variable in arithmetic operations
    3. Initializing the variable at declaration
    4. Using the variable as a loop counter

    Explanation: You cannot take the address of a 'register' variable because it may be stored in a CPU register, not memory. However, using it as a counter, in arithmetic, or initializing it at declaration are all allowed and typical uses. Only the address operation is fundamentally restricted by the register class.

  5. Effect of static on variable lifetime in a function

    How does declaring a variable as 'static' within a function, such as 'static int visits;', affect its lifetime?

    1. It cannot be initialized
    2. It is destroyed at the end of each function call
    3. It becomes accessible from outside the function
    4. Its value persists across multiple calls to the function

    Explanation: A static variable inside a function retains its value between function calls, offering function-level persistence. By contrast, regular (auto) variables are destroyed after each call. Declaring static does not provide wider accessibility outside the function, nor does it prohibit initialization.