I had been introduced to the C language in the very first day of my B.Sc. course back in 2003. Since then I have been using it for doing quite some varied kinds of programming jobs. But even after so many years, its every now and then that I find out something I did not know before. Here is one such thing that I encountered few days back.
I was taught by both my C teachers in B.Sc. as well as in B.Tech. that the modulus operator in C (denoted by the “%” sign) behaves exactly similar to the mod function that is defined in Mathematics. However here is something weird that I saw recently. Better to show it by code:
The output given is:
( 1)%( 6) = 1
(-1)%( 6) = -1
( 1)%(-6) = 1
(-1)%(-6) = -1
This does not match at all with the definition of the modulus operation that we have learnt in Mathematics classes. Specifically, modulus can never be negative. So something must be wrong. As it turns out C’s modulo operator behaves differently from the mathematically defined ones when we apply it for negative numbers.
The C90 standard does not define the result of modulus applied on negative numbers, so the result is compiler dependant. But C99 clearly says that the sign of the result is the same is that of the dividend, so in essence for something like:
c = a%b;
The actual operation done is:
c = sign(a) * ( abs(a) % abs(b) );
- sign(a)=1 for a>=0
- sign(a)=-1 for a<0