ProgrammingHardTCS NQT · Infosys

#define HALF(x) x / 2. With int a = 5, b = 3, what does printf("%d", HALF(a + b)) print?

  1. A. 4
  2. B. 3
  3. C. 6
  4. D. 5

Correct answer: C. 6

The expansion is a + b / 2, which is 5 + 1 = 6. The programmer intended (a + b) / 2 = 4, so the macro needs parentheses around x.

More C Programming questions

Exams that ask this topic