Storage class defaults in C: If you declare a structure variable inside a block without an explicit storage-class specifier, what is its default storage duration?

C Programming Structures, Unions, Enums Difficulty: Easy
Choose an option
  • A
    Automatic (block) storage by default for local declarations.
  • B
    Static storage by default for any struct variable.
  • C
    Register storage by default for structs.
  • D
    Thread storage by default.
  • E
    External linkage by default even inside a block.

Answer

Correct Answer: Automatic (block) storage by default for local declarations.

Explanation

Introduction / Context: C variables have storage duration depending on where and how they are declared. This question focuses on a structure variable declared inside a block with no storage-class specifier.

Given Data / Assumptions:

  • Declaration occurs inside a function block.
  • No keywords like static, extern, or register are used.

Concept / Approach: Local variables declared inside a block have automatic storage duration by default. They are created upon block entry and destroyed when the block is exited. This rule applies to any type, including structs.

Step-by-Step Solution: 1) Place the declaration inside a function or compound statement. 2) Omit storage keywords; the default is automatic. 3) The object has block scope and automatic lifetime. 4) Contrast with file-scope declarations, which by default have static storage duration.

Verification / Alternative check: Compile with a local struct variable and inspect lifetime via prints or address changes across calls; automatic objects get new storage each call unless declared static.

Why Other Options Are Wrong: Option B: Static requires the static keyword or file scope.
Option C: register is only a hint and not default.
Option D: Thread storage uses _Thread_local or similar, not default.
Option E: External linkage is about identifiers, not automatic lifetime.

Common Pitfalls: Confusing storage duration (automatic vs static) with linkage and scope; assuming structs behave differently from scalars.

Final Answer: Automatic storage duration for local (block-scope) struct variables.

Discussion & Comments
No comments yet. Be the first to comment!
Join Discussion