* better
[mascara-docs.git] / lang / C / the.ansi.c.programming.language / notes.accompany.ansi.c / sx5j.html
blobfddb465fe9fa3fa6a5e6904f8aaaf15597f622ed
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.10: Assignment Operators and Expressions</title>
10 <link href="sx5i.html" rev=precedes>
11 <link href="sx5k.html" rel=precedes>
12 <link href="sx5.html" rev=subdocument>
13 </head>
14 <body>
15 <H2>section 2.10: Assignment Operators and Expressions</H2>
17 page 50
18 <p>You may wonder what it means to say that
19 ``<I>expr</I><tt>&lt;sub&gt;</tt>1<tt>&lt;/sub&gt;</tt> is computed only once''
20 since in an assignment like
21 <pre> i = i + 2
22 </pre>we don't ``evaluate''
23 the <TT>i</TT> on the left hand side of the <TT>=</TT> at all,
24 we assign to it.
25 The distinction becomes important,
26 however, when
27 the left hand side
28 (<I>expr</I><tt>&lt;sub&gt;</tt>1<tt>&lt;/sub&gt;</tt>)
29 is more complicated than a simple variable.
30 For example,
31 we could add 2 to
32 each of
33 the <TT>n</TT> cells of an array <TT>a</TT>
34 with code like
35 <pre> int i = 0;
36 while(i &lt; n)
37 a[i++] += 2;
38 </pre>If we tried to use the expanded form,
39 we'd get
40 <pre> int i = 0;
41 while(i &lt; n)
42 a[i++] = a[i++] + 2;
43 </pre>and by trying to increment <TT>i</TT> twice within the same expression
44 we'd get (as we'll see) undesired, unpredictable,
45 and in fact undefined results.
46 (Of course,
47 a more natural form of this loop would be
48 <pre> for(i = 0; i &lt; n; i++)
49 a[i] += 2;
50 </pre>and with the increment of <TT>i</TT> moved out of the array subscript,
51 it wouldn't matter so much whether we used <TT>a[i] += 2</TT>
52 or <TT>a[i] = a[i] + 2</TT>.)
53 </p><p>page 51
54 </p><p>To make the point more clear,
56 the ``complicated expression'' without using <TT>+=</TT> would look like
57 <pre> yyval[yypv[p3+p4] + yypv[p1+p2]] = yyval[yypv[p3+p4] + yypv[p1+p2]] + 2
58 </pre>(What's going on here is that the subexpression
59 <TT>yypv[p3+p4] + yypv[p1+p2]</TT> is being used as a subscript
60 to determine which cell of the <TT>yyval</TT> array to increment by 2.)
62 </p><p>The sentence
63 on p. 51
64 that includes the words
65 ``the assignment statement has a value''
66 is a bit misleading:
67 an assignment is really an expression in C.
68 Like any expression,
69 it has a value,
70 and it can therefore participate as a subexpression
71 in a larger expression.
72 (If the distinction between the terms ``statement'' and
73 ``expression''
74 seems vague,
75 don't worry;
76 we'll start talking about statements in the next chapter.)
77 </p><hr>
78 <p>
79 Read sequentially:
80 <a href="sx5i.html" rev=precedes>prev</a>
81 <a href="sx5k.html" rel=precedes>next</a>
82 <a href="sx5.html" rev=subdocument>up</a>
83 <a href="top.html">top</a>
84 </p>
85 <p>
86 This page by <a href="http://www.eskimo.com/~scs/">Steve Summit</a>
87 // <a href="copyright.html">Copyright</a> 1995, 1996
88 // <a href="mailto:scs@eskimo.com">mail feedback</a>
89 </p>
90 </body>
91 </html>