GATE Computer ScienceHardGATE CS (PYQ pattern)

int a = -17, b = 5; unsigned u = 5; /* 32-bit int and unsigned, C99 or later */ printf("%d %d %u", a / b, a % -b, a / u); What is printed?

  1. A. -3 -2 858993455
  2. B. -4 3 858993455
  3. C. -3 -2 4294967293
  4. D. -3 2 858993455

Correct answer: A. -3 -2 858993455

Since C99 integer division truncates toward zero: -17 / 5 = -3, and a % b takes the sign of the dividend: -17 % -5 = -2 (so that (a / -b) * -b + a % -b = 15 - 17 = -2 checks). In a / u the int is converted to unsigned: 4294967279 / 5 = 858993455, not (unsigned)(-3) = 4294967293.

More Programming and Data Structures (GATE) questions

Exams that ask this topic