ANSI C99 6.5.5 Multiplicative operators-
6.5.5.5: The result of the /
operator is the quotient from the division of the first operand by the second; the result of the %
operator is the remainder. In both operations, if the value of the second operand is zero, the behavior is undefined.
6.5.5.6: When integers are divided, the result of the /
operator is the algebraic quotient with any fractional part discarded (*90). If the quotient a/b
is representable, the expression (a/b)*b + a%b
shall equal a
.
*90: This is often called "truncation toward zero".
The type of modulo behavior you're thinking of is called "modular arithmetic" or "number theory" style modulo / remainder. Using the modular arithmetic / number theory definition of the modulo operator, it is non-sensical to have a negative result. This is (obviously) not the style of modulo behavior defined and used by C99. There's nothing "wrong" with the C99 way, it's just not what you were expecting. :)