In C99 and later, the expression -7 % 3 is evaluated. What value results, and which rule fixes it?
Correct answer
D. -1, because division truncates toward zero, so the remainder keeps the dividend's sign
15 views
D. -1, because division truncates toward zero, so the remainder keeps the dividend's sign
15 views
Truncating division in C99 makes -7/3 equal -2, and the identity (a/b)*b + a%b == a then forces -7%3 to be -1, so the remainder carries the dividend's sign. Python differs: its // floors, so -7%3 yields 2. The exam trap is assuming a remainder is always non-negative when a negative operand appears.
Asked in: NTS, PPSC IT, Campus Interviews
Not sure why the answer is D? Ask here and get an explanation.