section 2.5: Arithmetic Operators

page 41

Keep in the back of your mind somewhere the fact that the behavior of the / and % operators is not precisely defined for negative operands. This means that -7 / 4 might be -1 or -2, and -7 % 4 might be -3 or +1. The difference won't matter for the simple programs we'll be writing at first, but eventually you'll get bit by it if you don't remember it.

An additional arithmetic operation you might be wondering about is exponentiation. Some languages have an exponentiation operator (typically ^ or **), but C doesn't.

The term ``precedence'' refers to how ``tightly'' operators bind to their operands (that is, to the things they operate on). In mathematics, multiplication has higher precedence than addition, so 1 + 2 * 3 is 7, not 9. In other words, 1 + 2 * 3 is equivalent to 1 + (2 * 3). C is the same way.

The term ``associativity'' refers to the grouping when two or more operators of the same precedence participate next to each other in an expression. When an operator (like subtraction) associates ``left to right,'' it means that 1 - 2 - 3 is equivalent to (1 - 2) - 3 and gives -4, not +2.

By the way, the word ``arithmetic'' as used in the title of this section is an adjective, not a noun, and it's pronounced differently than the noun: the accent is on the third syllable.


Read sequentially: prev next up top

This page by Steve Summit // Copyright 1995, 1996 // mail feedback