What is printed by this C code: int x = 2 + 3 * 4 % 5; printf(“%d”, x);?
Correct answer A. 4
Explanation
*, / and % share the same precedence, higher than +, and they associate left to right. So 3 * 4 is evaluated first (12), then 12 % 5 gives 2, and finally 2 + 2 gives 4. The answer 0 comes from wrongly computing (2 + 3 * 4) % 5, and 14 ignores the % entirely. Learning precedence groups, not individual operators, avoids such mistakes.
Answer checked How we write and check questions
Discussion