* remove "\r" nonsense
[mascara-docs.git] / C / the.ansi.c.programming.language / c.programming.notes / sx2e.html
blob79441fe9712e0b86bde86bf9b88f0f08cef8b286
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. -->
5 <html>
6 <head>
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>
13 </head>
14 <body>
15 <H2>2.5 Arithmetic Operators</H2>
17 <p>[This section corresponds to K&amp;R Sec. 2.5]
18 </p><p>The basic operators for performing arithmetic are the same in
19 many computer languages:
20 <br>
21 <br>
22 <pre>
23 <TT>+</TT> addition
24 <TT>-</TT> subtraction
25 <TT>*</TT> multiplication
26 <TT>/</TT> division
27 <TT>%</TT> modulus (remainder)
28 </pre>
29 </p><p>
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
46 remainder
47 when two integers are divided:
48 <TT>1 % 2</TT> is 1;
49 <TT>7 % 4</TT> is 3.
50 (The modulus operator can only be applied to integers.)
51 </p><p>An additional arithmetic operation you might be wondering about
52 is exponentiation.
53 Some languages have an exponentiation operator
54 (typically <TT>^ or </TT><TT>**</TT>),
55 but C doesn't.
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>.
65 C is the same way.
66 </p><p>All of these operators ``group'' from left to right,
67 which means that
68 when two or
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.
72 For example,
73 <TT>1 - 2 - 3</TT> is equivalent to <TT>(1 - 2) - 3</TT>
74 and gives -4, not +2.
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,
82 you can always
83 use explicit parentheses.
84 For example,
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>.
87 </p><p>By the way,
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.
93 </p><hr>
94 <p>
95 Read sequentially:
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>
100 </p>
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>
105 </p>
106 </body>
107 </html>