ProgrammingHard

#define ADD(a, b) a + b printf("%d", ADD(1, 2) * 3); What is printed?

  1. A. 7
  2. B. 9
  3. C. 5
  4. D. Compile error: a macro cannot appear inside an expression

Correct answer: A. 7

A macro is pure text substitution, so ADD(1, 2) * 3 becomes 1 + 2 * 3 and * binds first: 7. 9 assumes the macro behaves like a function returning 3, and 5 applies the * 3 to a instead of b; the fix is #define ADD(a, b) ((a) + (b)).

More C Programming questions

Exams that ask this topic