Predict the output or error(s) if any for the code given below.
Qualcomm technical mcq question, verified with a worked answer. Free to practise - no sign-up.
Predict the output or error(s) if any for the code given below.
#include <stdio.h>
#define a 10
int main()
{
#define a 50
printf("%d", a);
return 0;
}
Show answer & explanation
Answer: B. 50
The preprocessor performs textual substitution of macros. The second #define a 50 redefines the macro a inside main(), which shadows the global definition #define a 10. At the point of printf("%d", a), the active definition is a = 50, so 50 is printed. The C preprocessor allows macro redefinition without error.
Step-by-step Derivation:
- Global scope:
#define a 10is defined. - Inside main():
#define a 50redefines the macroalocally (preprocessor scope, not lexical scope). - Preprocessor substitution happens before compilation: all occurrences of
aafter the second#defineare replaced with50. printf("%d", a)becomesprintf("%d", 50)after preprocessing.- Output: 50
Note: Redefining a macro is allowed in C. The preprocessor does not enforce unique macro names; the most recent definition in the preprocessing stream is used.