Multiple choice

Suppose a, b and c are integer variables assigned values 8, 3 and -5 respectively. In this case, the respective values of arithmetic expressions (a*c)%b and a*(c%b) are

  1. -1 & 16

  2. 1 & 16

  3. 1 & -16

  4. -1 & -16

Reveal answer Fill a bubble to check yourself
D Correct answer
Explanation

For (a*c)%b: (8*-5) = -40, then -40%3 = -1 (C's modulo preserves the sign of the dividend). For a*(c%b): -5%3 = -2, then 8*(-2) = -16. The key is that modulo with negative numbers gives negative remainders in C.