% is usually called the "mod" or "modulus" operator, but that means the same thing as remainder.
A couple of things to be careful of. The behavior of % with negative operands is not standardized. In
if a or b is negative, C only guarantees that the absolute value of the result will be less than b. Better make sure the numbers are positive. One way to do this is to add a multiple of the second operand to the first operand. But be careful of precedence.
The order of precedence of '%' is the same as for '*' and '/', which is higher than for '+' and '-'. So
evaluates the same as
which is just
Use parens to get what you want:
It also makes for more readable code.
-norm
__________________
Trenton Tornadoes 381
2004 Philadelphia Regional Winners
2006 Xerox Creativity Award
---
My corner of the
USPTO.
My favorite error message from gcc:
main is usually a function
My favorite error message from Windows:
There is not enough disk space available to delete this file.