What is printed by this C code: printf(“%d %d”, -7 / 2, -7 % 2);?
Correct answer D. -3 -1
Explanation
Since C99, integer division truncates towards zero, so -7 / 2 is -3, not -4. The remainder is defined so that (a / b) * b + a % b equals a: (-3) * 2 + r = -7 gives r = -1. So the result of % takes the sign of the dividend. The pair -4 1 would follow floor division, which C does not use, although languages such as Python do.
Answer checked How we write and check questions
Discussion