ProgrammingModerateProduct-company MCQ pattern

What is printed?

What is printed?
printf("%d %d %d %d", -17 / 5, -17 % 5, 17 / -5, 17 % -5);
  1. A. -4 3 -4 -3
  2. B. -3 2 -3 -2
  3. C. -3 -2 -3 2
  4. D. -3 -2 -4 2

Correct answer: C. -3 -2 -3 2

Since C99, integer division truncates toward zero, and a % b has the sign of a so that (a / b)·b + a % b == a: −17 / 5 = −3, −17 % 5 = −2; 17 / −5 = −3, 17 % −5 = 2. Python's floor division gives −4, 3, −4, −3 instead.

More C Programming questions

Exams that ask this topic