I don't know of any platform where sizeof(size_t) < sizeof(int), but it is permitted by C99. Index may wrap, There might also be a conversion of index to an int when it is returned, but again, the bits are preserved Which has no effect on the calculation, as the bits are preserved. If sizeof(size_t) = sizeof(int), then the expression (index + 1) converts the +1 to unsigned int. The C definition of the % operator implies the following behavior: If the quotient a/b is representable, the expression (a/b)*b + a%b shall equal a.ĭiscarding the fractional part of the remainder is often called truncation toward zero.
When integers are divided, the result of the / operator is the algebraic quotient with any fractional part discarded. In both operations, if the value of the second operand is zero, the behavior is undefined. 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. The C Standard, subclause 6.5.5, states: This behavior was changed in C99, and the change remains in C11.īecause not all C compilers are strictly C-conforming, programmers cannot rely on the behavior of the % operator if they need to run on a wide range of platforms with many different compilers. In C89 (and historical K&R implementations), the meaning of the remainder operator for negative operands was implementation-defined.