ProgrammingVery hardTCS NQT · Accenture

What is printed?

What is printed?
#include <stdio.h>
int main(void) {
    printf("%d %d %d", -7 / 2, -7 % 2, 7 % -2);
}
  1. A. -4 1 -1
  2. B. -3 -2 1
  3. C. -3 -1 1
  4. D. -2 -1 1

Correct answer: C. -3 -1 1

C99 and later truncate division toward zero, so -7 / 2 = -3 and -7 % 2 = -1; the remainder takes the sign of the dividend, so 7 % -2 = 1.

More C Programming questions

Exams that ask this topic