Mutually recursive macro definitions – will this program compile?\n\n#include <stdio.h>\n#define X (4+Y)\n#define Y (X+3)\n\nint main()\n{\n printf("%d\n", 4X + 2);\n return 0;\n}

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)
  • Expression used: 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 X4(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

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