1 <!DOCTYPE HTML PUBLIC
"-//W3O//DTD W3 HTML 2.0//EN">
2 <!-- This collection of hypertext pages is Copyright 1995-7 by Steve Summit. -->
3 <!-- This material may be freely redistributed and used -->
4 <!-- but may not be republished or sold without permission. -->
7 <link rev=
"owner" href=
"mailto:scs@eskimo.com">
8 <link rev=
"made" href=
"mailto:scs@eskimo.com">
9 <title>2.5 Arithmetic Operators
</title>
10 <link href=
"sx2d.html" rev=precedes
>
11 <link href=
"sx2f.html" rel=precedes
>
12 <link href=
"sx2.html" rev=subdocument
>
15 <H2>2.5 Arithmetic Operators
</H2>
17 <p>[This section corresponds to K
&R Sec.
2.5]
18 </p><p>The basic operators for performing arithmetic are the same in
19 many computer languages:
24 <TT>-
</TT> subtraction
25 <TT>*
</TT> multiplication
27 <TT>%
</TT> modulus (remainder)
30 The
<TT>-
</TT> operator can be used in two ways:
31 to subtract two numbers
32 (as in
<TT>a - b
</TT>),
33 or to negate one number
34 (as in
<TT>-a + b
</TT> or
<TT>a + -b
</TT>).
35 </p><p>When applied to integers,
36 the division operator
<TT>/
</TT> discards any remainder,
37 so
<TT>1 /
2</TT> is
0
38 and
<TT>7 /
4</TT> is
1.
39 But when either operand is a floating-point quantity
40 (type
<TT>float
</TT> or
<TT>double
</TT>),
41 the division operator yields a floating-point result,
42 with a potentially nonzero fractional part.
43 So
<TT>1 /
2.0</TT> is
0.5,
44 and
<TT>7.0 /
4.0</TT> is
1.75.
45 </p><p>The
<dfn>modulus
</dfn> operator
<TT>%
</TT> gives you the
47 when two integers are divided:
50 (The modulus operator can only be applied to integers.)
51 </p><p>An additional arithmetic operation you might be wondering about
53 Some languages have an exponentiation operator
54 (typically
<TT>^ or
</TT><TT>**
</TT>),
56 (To square or cube a number,
57 just multiply it by itself.)
58 </p><p>Multiplication, division, and modulus all have higher
59 <dfn>precedence
</dfn> than addition and subtraction.
60 The term ``precedence'' refers to how ``tightly'' operators bind to
61 their operands (that is, to the things they operate on).
62 In mathematics, multiplication has higher precedence than
63 addition, so
<TT>1 +
2 *
3</TT> is
7, not
9.
64 In other words,
<TT>1 +
2 *
3</TT> is equivalent to
<TT>1 + (
2 *
3)
</TT>.
66 </p><p>All of these operators ``group'' from left to right,
69 more of them have the same precedence and participate next to each
70 other in an expression,
71 the evaluation conceptually proceeds from left to right.
73 <TT>1 -
2 -
3</TT> is equivalent to
<TT>(
1 -
2) -
3</TT>
75 (``Grouping'' is sometimes called
<dfn>associativity
</dfn>,
76 although the term is used somewhat differently in programming
77 than it is in mathematics.
78 Not all C operators group from left to right;
79 a few group from right to left.)
80 </p><p>Whenever the default precedence or associativity
81 doesn't give you the grouping you want,
83 use explicit parentheses.
85 if you wanted to add
1 to
2 and then multiply the result by
3,
86 you could write
<TT>(
1 +
2) *
3</TT>.
88 the word ``arithmetic''
89 as used in the title of this section
90 is an adjective, not a noun,
91 and it's pronounced differently than the noun:
92 the accent is on the third syllable.
96 <a href=
"sx2d.html" rev=precedes
>prev
</a>
97 <a href=
"sx2f.html" rel=precedes
>next
</a>
98 <a href=
"sx2.html" rev=subdocument
>up
</a>
99 <a href=
"top.html">top
</a>
102 This page by
<a href=
"http://www.eskimo.com/~scs/">Steve Summit
</a>
103 //
<a href=
"copyright.html">Copyright
</a> 1995-
1997
104 //
<a href=
"mailto:scs@eskimo.com">mail feedback
</a>