* better
[mascara-docs.git] / lang / C / the.ansi.c.programming.language / notes.accompany.ansi.c / sx5h.html
bloba81b4240d655d2b2e773e1bbc0abb7802719879e
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. -->
5 <html>
6 <head>
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>
13 </head>
14 <body>
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>
19 will probably
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,
24 and it's
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,
30 it's an expression;
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:
34 <pre> int k = i + j;
35 k++;
36 </pre>But really,
37 when you want to add one to an expression, just use
38 <pre> i + j + 1
39 </pre></p><p>Another unfortunate
40 (and utterly meaningless)
41 example is
42 <pre> i = i++;
43 </pre>If you want to increment <TT>i</TT>
44 (that is,
45 add one to it,
46 and store the result back in <TT>i</TT>),
47 either use
48 <pre> i = i + 1;
49 </pre>or
50 <pre> i++;
51 </pre>Don't try to combine the two.
52 </p><p>page 47
53 </p><p>Deep sentence:
54 <blockquote>In a context where no value is wanted,
55 just the incrementing effect,
56 as in
57 <pre> if(c == '\n')
58 nl++;
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,
64 as in the examples
65 we'll look at later,
66 using one or the other makes a big difference.
67 </p><p>In that light,
68 study
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>)
74 were used.
75 </p><p>You may note that all three examples
76 on pages 47-48
77 use the postfix form.
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
85 on pages 47 and 48,
86 it means that the function does not return a value.
87 (This was briefly mentioned on page 30 in chapter 1.)
89 </p><hr>
90 <p>
91 Read sequentially:
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>
96 </p>
97 <p>
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>
101 </p>
102 </body>
103 </html>