OA. free
Free
Qualcomm Embedded Systems & Hardware Embedded Systems & Hardware Medium

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;
}
Choose one option.
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:

  1. Global scope: #define a 10 is defined.
  2. Inside main(): #define a 50 redefines the macro a locally (preprocessor scope, not lexical scope).
  3. Preprocessor substitution happens before compilation: all occurrences of a after the second #define are replaced with 50.
  4. printf("%d", a) becomes printf("%d", 50) after preprocessing.
  5. 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.