Arithmetic Operators
Operator |
Meaning |
Example |
Result |
* |
Multiplication |
x * y |
The product of |
/ |
Division |
x / y |
The quotient of |
% |
Modulo division |
x % y |
The remainder of the division |
+ |
Addition |
x + y |
The sum of |
- |
Subtraction |
x - y |
The difference of |
|
Positive sign |
+x |
The value of |
|
Negative sign |
-x |
The arithmetic negation of |
++ |
Increment |
++x x++ |
|
-- |
Decrement |
--x x-- |
|
The operands of arithmetic operators may have any arithmetic type.
Only the %
operator requires integer operands.
The usual arithmetic conversions may be
performed on the operands. For example, 3.0/2
is
equivalent to 3.0/2.0
. The result has the type of
the operands after such conversion.
Note that the result of division with integer operands is also an integer! For example:
6 / 4 // Result: 1 6 % 4 // Result: 2 6.0 / 4.0 // Result: 1.5
The increment operator
++
(and analogously, the decrement operator
--
) can be placed either
before or after its
operand. A variable x
is incremented (i. e.,
increased by 1) both by ++x ...
Get C Pocket Reference now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.