* remove "\r" nonsense
[mascara-docs.git] / C / the.ansi.c.programming.language / c.programming.notes / sx7b.html
blob0d0d18b9dd4f24d7a6e2ce8157ee1e384e1d75cc
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>7.2 Increment and Decrement Operators</title>
10 <link href="sx7a.html" rev=precedes>
11 <link href="sx7c.html" rel=precedes>
12 <link href="sx7.html" rev=subdocument>
13 </head>
14 <body>
15 <H2>7.2 Increment and Decrement Operators</H2>
17 <p>[This section corresponds to K&amp;R Sec. 2.8]
18 </p><p>The assignment operators
19 of the previous section
20 let us replace
21 <I>v</I> <TT>=</TT> <I>v</I> <I>op</I> <I>e</I>
22 with
23 <I>v</I> <I>op</I><TT>=</TT> <I>e</I>,
24 so that we didn't have to mention <I>v</I> twice.
25 In the most common cases,
26 namely
27 when we're adding or subtracting the constant 1
28 (that is, when <I>op</I> is <TT>+</TT> or <TT>-</TT> and <I>e</I> is 1),
29 C provides another set of shortcuts:
30 the <dfn>autoincrement</dfn> and <dfn>autodecrement</dfn> operators.
31 In their simplest forms, they look like this:
32 <pre>
33 ++i <I>add 1 to</I> i
34 --j <I>subtract 1 from</I> j
35 </pre>
36 These correspond to the slightly longer <TT>i += 1</TT>
37 and <TT>j -= 1</TT>,
38 respectively,
40 and also to the fully
41 ``longhand'' forms <TT>i = i + 1</TT>
42 and
43 <TT>j = j - 1</TT>.
44 </p><p>The <TT>++</TT> and <TT>--</TT> operators apply to one operand
45 (they're <dfn>unary</dfn> operators).
46 The expression <TT>++i</TT> adds 1 to <TT>i</TT>,
47 and stores the incremented result back in <TT>i</TT>.
49 This means that these operators don't just compute new values;
50 they also modify the value of some variable.
51 (They share this property--modifying some variable--with
52 the assignment operators;
53 we can say that these operators all have <dfn>side effects</dfn>.
54 That is, they have some effect, on the side,
55 other than just computing a new value.)
56 </p><p>The incremented
57 (or decremented)
58 result is also made available to the rest of
59 the expression,
60 so an expression like
61 <pre>
62 k = 2 * ++i
63 </pre>
64 means
65 ``add one to <TT>i</TT>,
66 store the result back in <TT>i</TT>,
67 multiply it by 2,
68 and store <em>that</em> result in <TT>k</TT>.''
69 (This is a pretty meaningless expression;
70 our actual uses of <TT>++</TT> later will make more sense.)
71 </p><p>Both the <TT>++</TT> and <TT>--</TT> operators have an unusual property:
72 they can be used in two ways,
73 depending on whether they are written
74 to the left or the right of
75 the variable
77 they're operating on.
78 In either case,
79 they increment or decrement the variable
81 they're operating on;
82 the difference concerns whether it's the old or the new value
83 that's ``returned'' to the surrounding expression.
84 The <dfn>prefix</dfn> form <TT>++i</TT>
85 increments <TT>i</TT> and returns the incremented value.
86 The <dfn>postfix</dfn> form <TT>i++</TT>
87 increments <TT>i</TT>,
88 but returns the <em>prior</em>, non-incremented value.
89 Rewriting our previous example slightly,
90 the expression
91 <pre>
92 k = 2 * i++
93 </pre>
94 means
95 ``take <TT>i</TT>'s old value and multiply it by 2,
96 increment <TT>i</TT>,
97 store the result of the multiplication in <TT>k</TT>.''
98 </p><p>The distinction between the prefix and postfix forms of
99 <TT>++</TT> and <TT>--</TT>
100 will probably
101 seem strained at first,
102 but it will make more sense once we begin using these operators
103 in more realistic situations.
104 </p><p>For example,
105 our <TT>getline</TT> function
106 of the previous chapter
107 used the statements
108 <pre>
109 line[nch] = c;
110 nch = nch + 1;
111 </pre>
112 as the body of its inner loop.
113 Using the <TT>++</TT> operator,
114 we could simplify this to
115 <pre>
116 line[nch++] = c;
117 </pre>
118 We wanted to increment <TT>nch</TT> <em>after</em>
119 deciding which element of the <TT>line</TT> array to store into,
120 so the postfix form <TT>nch++</TT> is
121 appropriate.
122 </p><p>
123 Notice that it only makes sense to apply the <TT>++</TT> and
124 <TT>--</TT> operators to variables
125 (or to other ``containers,'' such as <TT>a[i]</TT>).
126 It would be meaningless to say something like
127 <pre>
129 </pre>
131 <pre>
132 (2+3)++
133 </pre>
134 The <TT>++</TT> operator doesn't just mean ``add one'';
135 it means ``add one <em>to a variable</em>''
136 or ``make a variable's value one more than it was before.''
137 But <TT>(1+2)</TT> is not a variable,
138 it's an expression;
139 so there's no place for <TT>++</TT> to store the incremented result.
140 </p><p>Another unfortunate
141 example is
142 <pre>
143 i = i++;
144 </pre>
145 which some confused programmers sometimes write,
146 presumably because they want to
147 be extra sure that
148 <TT>i</TT> is incremented by 1.
149 But <TT>i++</TT> all by itself is sufficient to increment <TT>i</TT> by 1;
150 the extra
151 (explicit)
152 assignment to <TT>i</TT> is unnecessary
153 and in fact counterproductive, meaningless, and incorrect.
154 If you want to increment <TT>i</TT>
155 (that is,
156 add one to it,
157 and store the result back in <TT>i</TT>),
158 either use
160 <pre>
161 i = i + 1;
163 i += 1;
165 ++i;
167 i++;
168 </pre>
169 Don't try to use some bizarre combination.
170 </p><p>Did it matter whether we used
171 <TT>++i</TT> or <TT>i++</TT> in this last example?
172 Remember, the difference between
173 the two forms
174 is what value
175 (either the old or the new) is passed on to the surrounding expression.
176 If there is no surrounding expression,
177 if the <TT>++i</TT> or <TT>i++</TT> appears all by itself,
178 to increment <TT>i</TT> and do nothing else,
179 you can use either form;
180 it makes no difference.
181 (Two ways that an expression can appear ``all by itself,''
182 with ``no surrounding expression,''
183 are when it is an expression statement terminated by a semicolon, as above,
184 or when it is one of the controlling expressions of a <TT>for</TT> loop.)
185 For example, both the loops
186 <pre>
187 for(i = 0; i &lt; 10; ++i)
188 printf("%d\n", i);
189 </pre>
191 <pre>
192 for(i = 0; i &lt; 10; i++)
193 printf("%d\n", i);
194 </pre>
195 will behave exactly the same way and produce exactly the same results.
196 (In real code,
197 postfix increment is probably more common,
198 though prefix definitely has its uses, too.)
199 </p><p>In the preceding section,
200 we simplified the expression
201 <pre>
202 a[d1 + d2] = a[d1 + d2] + 1;
203 </pre>
204 from a previous chapter
205 down to
206 <pre>
207 a[d1 + d2] += 1;
208 </pre>
209 Using <TT>++</TT>,
210 we could simplify it still further to
211 <pre>
212 a[d1 + d2]++;
213 </pre>
215 <pre>
216 ++a[d1 + d2];
217 </pre>
218 (Again,
219 in this case,
220 both are equivalent.)
221 </p><p>We'll see more examples of these operators
222 in the next section
224 in the next chapter.
225 </p><hr>
227 Read sequentially:
228 <a href="sx7a.html" rev=precedes>prev</a>
229 <a href="sx7c.html" rel=precedes>next</a>
230 <a href="sx7.html" rev=subdocument>up</a>
231 <a href="top.html">top</a>
232 </p>
234 This page by <a href="http://www.eskimo.com/~scs/">Steve Summit</a>
235 // <a href="copyright.html">Copyright</a> 1995-1997
236 // <a href="mailto:scs@eskimo.com">mail feedback</a>
237 </p>
238 </body>
239 </html>