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>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
>
15 <H2>7.2 Increment and Decrement Operators
</H2>
17 <p>[This section corresponds to K
&R Sec.
2.8]
18 </p><p>The assignment operators
19 of the previous section
21 <I>v
</I> <TT>=
</TT> <I>v
</I> <I>op
</I> <I>e
</I>
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,
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:
34 --j
<I>subtract
1 from
</I> j
36 These correspond to the slightly longer
<TT>i +=
1</TT>
41 ``longhand'' forms
<TT>i = i +
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
58 result is also made available to the rest of
65 ``add one to
<TT>i
</TT>,
66 store the result back in
<TT>i
</TT>,
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
79 they increment or decrement the variable
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,
95 ``take
<TT>i
</TT>'s old value and multiply it by
2,
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>
101 seem strained at first,
102 but it will make more sense once we begin using these operators
103 in more realistic situations.
105 our
<TT>getline
</TT> function
106 of the previous chapter
112 as the body of its inner loop.
113 Using the
<TT>++
</TT> operator,
114 we could simplify this to
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
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
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,
139 so there's no place for
<TT>++
</TT> to store the incremented result.
140 </p><p>Another unfortunate
145 which some confused programmers sometimes write,
146 presumably because they want to
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;
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>
157 and store the result back in
<TT>i
</TT>),
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
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
187 for(i =
0; i
< 10; ++i)
192 for(i =
0; i
< 10; i++)
195 will behave exactly the same way and produce exactly the same results.
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
202 a[d1 + d2] = a[d1 + d2] +
1;
204 from a previous chapter
210 we could simplify it still further to
220 both are equivalent.)
221 </p><p>We'll see more examples of these operators
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>
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>