What is printed by printf("%d", 7 / 2); in C when both operands are of type int?
Correct answer
D. 3, because integer division truncates toward zero
19 views
D. 3, because integer division truncates toward zero
19 views
When both operands of / are integers, C performs integer division and discards the fractional part, truncating toward zero, so 7 / 2 yields 3. No rounding to nearest occurs, and 7 / 2.0 would give 3.5 because one operand is then a double. This truncation is also why the idiom (a + b - 1) / b is used to round a division upward.
Asked in: NTS, PPSC IT, Campus Interviews
Not sure why the answer is D? Ask here and get an explanation.