1 <!DOCTYPE HTML PUBLIC
"-//W3O//DTD W3 HTML 2.0//EN">
2 <!-- This collection of hypertext pages is Copyright 1995, 1996 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>section
2.8: Increment and Decrement Operators
</title>
10 <link href=
"sx5g.html" rev=precedes
>
11 <link href=
"sx5i.html" rel=precedes
>
12 <link href=
"sx5.html" rev=subdocument
>
15 <H2>section
2.8: Increment and Decrement Operators
</H2>
17 <p>The distinction between the prefix and postfix forms of
18 <TT>++
</TT> and
<TT>--
</TT>
20 seem strained at first,
21 but it will make more sense once we begin using these operators
22 in more realistic situations.
23 </p><p>The authors point out that an expression like
<TT>(i+j)++
</TT> is illegal,
25 worth thinking for a moment about
<em>why
</em>.
26 The
<TT>++
</TT> operator doesn't just mean ``add one'';
27 it means ``add one
<em>to a variable
</em>''
28 or ``make a variable's value one more than it was before.''
29 But
<TT>(i+j)
</TT> is not a variable,
31 so there's no place for
<TT>++
</TT> to store the incremented result.
32 If you were bound and determined to use
<TT>++
</TT> here,
33 you'd have to introduce another variable:
37 when you want to add one to an expression, just use
39 </pre></p><p>Another unfortunate
40 (and utterly meaningless)
43 </pre>If you want to increment
<TT>i
</TT>
46 and store the result back in
<TT>i
</TT>),
51 </pre>Don't try to combine the two.
54 <blockquote>In a context where no value is wanted,
55 just the incrementing effect,
59 </pre>prefix and postfix are the same.
60 </blockquote>In other words,
61 when you're just incrementing some variable,
62 you can use either the
<TT>nl++
</TT> or
<TT>++nl
</TT> form.
63 But when you're immediately using the result,
66 using one or the other makes a big difference.
69 one of the examples on this page--
<TT>squeeze
</TT>,
70 the modified
<TT>getline
</TT>,
71 or
<TT>strcat
</TT>--and convince yourself that it would
72 <em>not
</em> work if the wrong form of increment
73 (
<TT>++i
</TT> or
<TT>++j
</TT>)
75 </p><p>You may note that all three examples
78 Postfix increment is probably more common,
79 though prefix definitely has its uses, too.
80 </p><p>You may notice the keyword
<TT>void
</TT> popping up in a few code examples.
81 <TT>void
</TT> is a type we haven't met yet;
82 it's a type with no values and no operations.
83 When a function is declared as ``returning''
<TT>void
</TT>,
84 as in the
<TT>squeeze
</TT> and
<TT>strcat
</TT> examples
86 it means that the function does not return a value.
87 (This was briefly mentioned on page
30 in chapter
1.)
92 <a href=
"sx5g.html" rev=precedes
>prev
</a>
93 <a href=
"sx5i.html" rel=precedes
>next
</a>
94 <a href=
"sx5.html" rev=subdocument
>up
</a>
95 <a href=
"top.html">top
</a>
98 This page by
<a href=
"http://www.eskimo.com/~scs/">Steve Summit
</a>
99 //
<a href=
"copyright.html">Copyright
</a> 1995,
1996
100 //
<a href=
"mailto:scs@eskimo.com">mail feedback
</a>