I seem to remember that ANSI C didn’t specify what value should be returned when either operand of a modulo operator is negative (just that it should be consistent). Did it get specified later, or was it always specified and I am remembering incorrectly?
Share
C89, not totally (§3.3.5/6). It can be either -5 or 5, because -5 / 10 can return 0 or -1 (
%is defined in terms of a linear equation involving/,*and+):C99, yes (§6.5.5/6), the result must be -5:
Similarly, in C++98 the result is implementation defined (§5.6/4), following C89’s definition, but mentions that the round-towards-zero rule is preferred,
and indeed it becomes the standard rule in C++0x (§5.6/4):