Difficulty: Easy
Correct Answer: No — recursive macro expansion causes a preprocessing error
Explanation:
Introduction / Context:
Here two macros are defined in terms of each other: X
uses Y
and Y
uses X
. The question is whether such cyclic macro definitions are acceptable and what the preprocessor does when asked to expand them.
Given Data / Assumptions:
#define X (4+Y)
#define Y (X+3)
4X + 2
.
Concept / Approach:
When the preprocessor expands X
, it replaces it with (4+Y)
and then attempts to expand Y
, which requires expanding X
again, and so on. This mutual recursion leads to non-terminating expansion. Conforming preprocessors detect self or mutual recursion and report an error such as “macro recursion” or “unterminated recursion in macro expansion.”
Step-by-Step Solution:
Start with 4X + 2
→ expand X
→ 4(4+Y) + 2
.Now expand Y
inside → (X+3)
appears → expansion requires X
again.This cycle repeats indefinitely, so the preprocessor aborts with an error.
Verification / Alternative check:
Replace one macro with a literal (e.g., #define Y 5
) and compilation succeeds. Observing preprocessed output shows the expansion process.
Why Other Options Are Wrong:
Compiles and prints — impossible due to expansion loop. Undefined value — the program never compiles, so no runtime value exists. Depends on optimization — preprocessing happens before optimization.
Common Pitfalls:
Creating accidental recursive macros via layered headers; assuming macros behave like variables with fixed values; forgetting that macro expansion is purely textual and must terminate.
Final Answer:
No — recursive macro expansion causes a preprocessing error
Discussion & Comments