Previous Table of Contents Next


3.3.2. Arithmetic Operators

The basic arithmetic operators in C, as in many other languages, are +, -, *, and /, which are respectively addition, subtraction, multiplication, and division. These are all binary operators, combining two subexpressions to form a larger expression. They can all be used with integer or floating-point operands. (The + and - operators can also be used with pointers, as discussed in section 3.6.3.) The minus sign (-) can also be used as a unary negation operator, meaning that it is applied to just one operand; the expression -a simply negates the value of a, and is equivalent to 0 - a. For symmetry, the + operator can also be used in a unary form, although there is rarely any reason to do so.

The division operator, /, has the not-uncommon property that it operates in one of two different ways, depending on whether its operands are integers or floating-point values. If both its operands are integers, an integer division is performed, and the remainder is discarded. This means that 7 / 2 is 3 (not 3.5, not 3 1/2, not “three remainder one”). If either or both of the operands are floating-point values, however, floating-point division is performed, yielding a floating-point result. Therefore, the divisions 7.0/2, 7/2.0, and 7.0/2.0 all yield 3.5. When floating-point division is required in a particular situation, therefore, it is necessary to ensure that one of the operands is a floating-point constant or a variable of floating-point type, or to use an explicit type conversion (see section 3.3.11).

(As a matter of curiosity, C uses * and / for multiplication and division for the same reason that most conventional computer languages do: the symbols × and ÷ do not appear on most computer keyboards or in the ASCII character set.)

Included in the category of arithmetic operators is a fifth operator, the “remainder” or modulus operator %. The % operator may be applied only to integers, and yields the remainder that is left when the two operands are divided. Therefore, 7 % 2 is 1, 123 % 100 is 23, and 4 % 2 is 0. Among other things, the % operator is useful for determining whether one number is an even multiple of another, in which case the remainder when dividing the first by the second is 0.

One caveat: Integer division and the modulus operator are not precisely defined when one or both of the operands are negative. On different machines, -7 / 2 might give -3 or -4, and -7 % 2 might give 1 or -1. (However, it is guaranteed that for any integers a and b, (a/b)*b + (a%b) is equal to a, as long as b is not 0, of course.)

When evaluating expressions, C applies rules of precedence and associativity, just as most computer languages do, and as are present in the rules of algebra. (However, “associativity” has a significantly different meaning in programming than it does in mathematics.) Multiplication, division, and the modulus operator all have higher precedence than addition and subtraction, which means that the expression

a + b * c

is interpreted as

a + (b * c)

In other words, the operators *, /, and % “bind more tightly” than + and -. (One might also say that the multiplication in a + b * c happens “before” the addition, but as mentioned in section 3.3.9, using words like “before” when discussing precedence can be misleading.) Unary operators, including unary -, have the highest precedence of all, which means that

-a + b

is interpreted as

(-a) + b

not -(a + b).

The five binary operators in this section all “group” from left to right, which means that

a + b + c

is interpreted as

(a + b) + c

and that

a - b - c

is interpreted as

(a - b) - c

not as a - (b - c). Choosing a particular interpretation for subtraction and division is obviously significant, but it can also make a difference for addition and multiplication. For example, in the expression a + b + c, if it is the case that b + c would overflow, but (a + b) + c would not (perhaps a is -1, b is 1, and c is 32767), the programmer can be assured that overflow will not occur even in the absence of explicit parentheses.

To override the default precedence or associativity and force a particular interpretation, explicit parentheses can be used; for example, the expression

(a + b) * c

requests that a be added to b and the result multiplied by c.


Previous Table of Contents Next