how - 5%3 is equal to - 2?

586 views Asked by At

I'm learning C basics right now. I have a question which is confusing me little bit.
My question is how the below program's output is - 2 ?

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

There are 1 answers

1
LPs On

The % operator is gives you the remainder left after of integer division. Then -5/3 = -1 with -2 as remainder of division as 3*(-1)=-3 and -5-(-3)=-5+3=-2.