-3

I have the following code

#include <stdio.h>

int main(void)
{

    printf("%d\n", -8%5);


    printf("%d\n", 8%-5);

    printf("%d\n", -8%-5);


    return 0;
}

The output I get is

-3
3
-3

How is it that -8%5 and 8%-5 have different signs, especially when the results of -8/5 and 8/-5 have the same output ?

Also why does -8%-5 give -3 as the output, when -8/-5 gives it's output as 1 ?

Nathu
  • 449
  • 1
  • 4
  • 11

1 Answers1

0

If both operands are nonnegative then the remainder is nonnegative; if not, the sign of the remainder is implementation-defined.

msc
  • 33,420
  • 29
  • 119
  • 214