Can I rely on % (modulo) operator in C for negative numbers?
问题 Using GCC: printf("%i \n", -1 % (int)4); printf("%u \n", -1 % (unsigned int)4); Output: -1 3 Can I rely on this behaviour across platforms? Should I explicitly define MOD and REM macros to be sure this isn't altered? 回答1: From C99 onwards the result of % is required to be rounded toward 0 as quoted by Chris Dodd. Prior to C99 standard, % operator's behavior on negative number is implementation defined . When integers are divided and the division is inexact, if both operands are positive the