ProgrammingHardProduct company OA

What is printed?

What is printed?
#define X 3
#define Y X * 2
#if Y == 6
  #define Z 1
#else
  #define Z 2
#endif
printf("%d %d", Z, Y + 1 * 2);
  1. A. 1 7
  2. B. 1 14
  3. C. 2 8
  4. D. 1 8

Correct answer: D. 1 8

In #if, macros expand too: Y becomes 3 * 2 = 6, so Z is 1. In the printf, Y + 1 * 2 becomes 3 * 2 + 1 * 2 = 8. '14' treats Y as a parenthesised 6 and then multiplies (6 + 1) * 2.

More C Programming questions

Exams that ask this topic