Question discussion

What is the output of the following C program (assume C99 or later)?#include int main() { printf("%d %d", -7 / 2, -7 % 2); return 0; }

2308Medium DSSSB TGT Computer Science 0 replies 1 views
0

What is the output of the following C program (assume C99 or later)?

#include <stdio.h>
int main() {
    printf("%d %d", -7 / 2, -7 % 2);
    return 0;
}
  • A-4 1
  • B-4 -1
  • C-3 1
  • D-3 -1
Correct answer: D

Explanation

Since C99 integer division truncates towards zero, so -7 / 2 is -3 and the remainder has the sign of the dividend, so -7 % 2 is -1.

Discussion 0

Nobody has replied yet. Start the discussion!