#define a 10 int main() { #define a 50 printf("%d",a); getchar(); return 0; }
#define a 10
int main()
{
#define a 50
printf("%d",a);
getchar();
return 0;
}
Answer: C. 50 — The C preprocessor performs macro substitution before compilation begins. When an object-like macro is redefined with a different replacement text and no…
- A.
10
- B.
60
- C.
50
- D.
error
Attempted by 42 students.
Show answer & explanation
Correct answer: C
The C preprocessor performs macro substitution before compilation begins. When an object-like macro is redefined with a different replacement text and no #undef precedes it, the C standard requires only a diagnostic — most compilers, including GCC, satisfy this with a WARNING and still compile successfully, updating the macro to the newest definition.
The identifier a is first defined as 10, outside main.
Inside main, a is redefined as 50 before it is used anywhere in the code.
Because the printf call appears after this second #define, the preprocessor substitutes a with the text of the LATEST definition seen up to that point — not the first one.
The compiler emits a 'redefined' warning for the differing macro but still completes compilation and produces a running program.
GCC's own documentation on "Undefining and Redefining Macros" confirms this exact behaviour: a non-identical redefinition issues a warning and the preprocessor switches to the new definition — the build does not fail, and the substituted value is the most recent one. This matches the printed output.
Output: 50