0

I have this code:


#include <stdio.h>
int main(){
    int a = 5 % -2;
    printf("%d", a);
    return 0;
}

Normally, it should return me -1 as an output. However, it is returning 1 instead.Why??

Faiyaz Ahmad
  • 87
  • 1
  • 6
  • *Normally, it should return me* - what does it mean "normally"? This operator is well defined *"When integers are divided, the result of the / operator is the algebraic quotient with any fractional part discarded. If the quotient a/b is representable, the expression (a/b)*b + a%b shall equal a;"* Here `a/b` is `-2`, therefore `(a/b)*b + a%b = -2*-2 +1 == 5 = a` - as required – Eugene Sh. Dec 10 '20 at 16:22
  • Possible duplicate of https://stackoverflow.com/questions/11720656/modulo-operation-with-negative-numbers – LoPiTaL Dec 10 '20 at 16:23
  • 2
    The `%` is not a direct implementation of the mathematical *mod*. – Weather Vane Dec 10 '20 at 16:30

0 Answers0