Arithmetic Operators

Table 1-9. The arithmetic operators

Operator

Meaning

Example

Result

*

Multiplication

x * y

The product of x and y.

/

Division

x / y

The quotient of x by y.

%

Modulo division

x % y

The remainder of the division x / y.

+

Addition

x + y

The sum of x and y.

-

Subtraction

x - y

The difference of x and y.

+ (unary)

Positive sign

+x

The value of x.

- (unary)

Negative sign

-x

The arithmetic negation of x.

++

Increment

++x
x++

x is incremented (x=x+1). The prefixed operator (++x) increments the operand before it is evaluated; the postfixed operator (x++) increments the operand after it is evaluated.

--

Decrement

--x
x--

x is decremented (x=x-1). The prefixed operator (--x) decrements the operand before it is evaluated; the postfixed operator (x--) decrements the operand after it is evaluated.

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.