ProgrammingVery hardGATE CS

What is printed? Assume C99 or later (division truncates towards zero).

What is printed? Assume C99 or later (division truncates towards zero).
#include <stdio.h>
int main(void) {
    printf("%d %d %d %d", -17 / 5, -17 % 5, 17 / -5, 17 % -5);
    return 0;
}
  1. A. -4 -2 -4 2
  2. B. -3 -2 -3 2
  3. C. -4 3 -4 -3
  4. D. -3 3 -3 -2

Correct answer: B. -3 -2 -3 2

Division truncates towards zero, and a % b has the sign of the dividend so that (a / b) * b + a % b == a. Hence -17 / 5 = -3 with remainder -2, and 17 / -5 = -3 with remainder 2.

More C Programming questions

Exams that ask this topic