C macros and scope – do macros have local scope by default?\nEvaluate the statement about the visibility and lifetime of macro definitions.

Difficulty: Easy

Correct Answer: Incorrect

Explanation:


Introduction / Context:
This question targets the scope of macro definitions in C. Understanding where a macro is visible helps prevent surprising substitutions and name clashes across files.



Given Data / Assumptions:

  • The statement says macros have local scope.
  • No #undef or conditional compilation is applied after the definition.


Concept / Approach:
Macros have translation-unit scope from the point of definition to the end of the file (subject to conditional compilation) unless they are explicitly undefined using #undef. They are not block-scoped like variables declared within a function or compound statement. When included via headers, macros affect all files that include those headers after the definition line.



Step-by-Step Solution:
Recognize that preprocessor works before compilation and does not honor C block scope.By default, a macro is visible in all subsequent code of the same translation unit.Therefore, the claim of “local scope” is false.



Verification / Alternative check:
Add #undef MAC to limit visibility; observe that substitutions cease beyond that line.



Why Other Options Are Wrong:
Correct only inside functions — macros are not tied to function scope. Correct only inside header files — headers simply inject definitions; scope is still translation-unit from definition onward.



Common Pitfalls:
Assuming macros honor braces; forgetting to #undef macros in headers; creating accidental global effects.



Final Answer:
Incorrect

Discussion & Comments

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