ProgrammingHardProduct-company MCQ pattern

What is printed?

What is printed?
#define ABS(x) x < 0 ? -x : x
printf("%d %d", ABS(2 - 5), ABS(-4) * 2);
  1. A. 3 8
  2. B. -7 8
  3. C. -7 4
  4. D. 3 4

Correct answer: C. -7 4

Macros substitute tokens with no brackets. ABS(2 - 5) becomes 2 - 5 < 0 ? -2 - 5 : 2 - 5; the condition is true and -2 - 5 = -7. ABS(-4) * 2 becomes -4 < 0 ? - -4 : -4 * 2; the * 2 attaches only to the last branch, so the result is 4. The fix is #define ABS(x) ((x) < 0 ? -(x) : (x)).

More C Programming questions

Exams that ask this topic