ProgrammingHardTCS NQT · Infosys pattern

What is printed?

What is printed?
#define TWICE(x) x + x
printf("%d", 2 * TWICE(3));
  1. A. 12
  2. B. 9
  3. C. 18
  4. D. 6

Correct answer: B. 9

TWICE(3) expands purely textually to 3 + 3. The full expression becomes 2 * 3 + 3, and by normal precedence (* before +) that is (2*3) + 3 = 6 + 3 = 9, not 2*(3+3)=12 as the macro name might suggest.

More C Programming questions

Exams that ask this topic