There are three binary operators in the multiplicative set. They are:
symbol | name | meaning |
---|---|---|
* | asterisk | multiplication |
/ | slash | division |
% | per-cent | remainder (or modulo) |
The first two, multiplication and division will work with either integer or floating operators. When integer operands and integer division is performed then an integer result is given. This result is the largest integer number with any remainder thrown away: 9/5 => 1.
The remainder operator, '%', can only be applied to integers. It gives the remainder from an integer division, that is what is thrown away or left after division is performed: 9%5 => 4. The implementers of the 'C' language have chosen that the modulo operator when applied to a negative number ignores the sign but preserves it, that is: (-x)%y == -(x%y), unfortunately this is not mathematically correct. The correct result for negative values is: (-x)%y == y-(x%y).
Unlike the practice in algebra, where multiplication is implied by just writing two numbers or variables or expressions next to each other, in 'C' (and nearly all programming languages) the multiplication operator must be explicitly written or else it will not be possible to lexically distinguish the tokens. Thus:
temp (x+5)
would not mean 'temp' times 'x+5', but would mean passing the value of 'x+5' to the function named 'temp'!
Clearly if one wrote:
ab
to mean 'a' times 'b' the compiler would have no way to distinguish this from the identifier 'ab'.
All three of these operators have the same precedence, so an expression like:
45/5*3
may not do what you expect. It is better in such a cases to use parenthesis:
(45/5)*3
or
45/(5*3)
which are, of course, different.