In C preprocessor macros, what will be the exact output of this program (note the macro parameter name and the printed literal)? #include<stdio.h> #define PRINT(int) printf("int=%d, ", int); int main() { int x = 2, y = 3, z = 4; PRINT(x); PRINT(y); PRINT(z); return 0; }

Difficulty: Easy

Correct Answer: int=2, int=3, int=4,

Explanation:


Introduction / Context:
This question tests your understanding of C preprocessor macros, argument substitution, and how string literals in printf determine the printed text. The parameter name of a macro can shadow language keywords without affecting compilation because the preprocessor runs before the compiler’s parsing stage. The key is to follow the expansion faithfully and read the literal being printed.


Given Data / Assumptions:

  • Macro: #define PRINT(int) printf("int=%d, ", int);
  • Variables: x = 2, y = 3, z = 4.
  • Each macro call expands to one printf invocation.


Concept / Approach:
The preprocessor replaces the macro parameter token int with the actual argument, but the literal text "int=%d, " remains unchanged. Thus, PRINT(x) becomes printf("int=%d, ", x);, which prints int=2, . The same happens for y and z. Using int as a macro parameter name is allowed; it does not conflict with the keyword in this context.


Step-by-Step Solution:

Expand PRINT(x)printf("int=%d, ", x); → prints int=2, .Expand PRINT(y) → prints int=3, .Expand PRINT(z) → prints int=4, .


Verification / Alternative check:
Replace the literal with "val=%d, " to see the difference; or expand the macro with gcc -E (preprocessing only) to confirm the textual substitution.


Why Other Options Are Wrong:

Repeated values (all 2s, all 3s, all 4s) would require passing the same argument each time, which we do not.“Compilation error” is incorrect; the macro parameter name may legally be int.


Common Pitfalls:
Thinking the word int in the literal will change with the argument; confusing macro parameter names with C keywords during compilation.


Final Answer:
int=2, int=3, int=4,.

More Questions from C Preprocessor

Discussion & Comments

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