* X more docs for C
[mascara-docs.git] / C / the.ansi.c.programming.language / c.programming.notes / sx2f.html
blob36034874aa5660e9b3e0ce4f897107fefe4e60cf
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>2.6 Assignment Operators</title>
10 <link href="sx2e.html" rev=precedes>
11 <link href="sx2g.html" rel=precedes>
12 <link href="sx2.html" rev=subdocument>
13 </head>
14 <body>
15 <H2>2.6 Assignment Operators</H2>
17 <p>[This section corresponds to K&amp;R Sec. 2.10]
18 </p><p>The assignment operator <TT>=</TT> assigns a value to a variable.
19 For example,
20 <pre>
21 x = 1
22 </pre>
23 sets <TT>x</TT> to 1,
24 and
25 <pre>
26 a = b
27 </pre>
28 sets <TT>a</TT> to whatever <TT>b</TT>'s value is.
29 The expression
30 <pre>
31 i = i + 1
32 </pre>
33 is, as we've mentioned elsewhere, the standard programming
34 idiom for increasing a variable's value by 1:
35 this expression takes <TT>i</TT>'s old value, adds 1 to it,
36 and stores it back into <TT>i</TT>.
37 (C provides several ``shortcut'' operators for
38 modifying variables in this and similar ways, which we'll meet
39 later.)
40 </p><p>We've called the <TT>=</TT> sign the ``assignment operator''
41 and referred to ``assignment expressions''
42 because, in fact, <TT>=</TT> <em>is</em> an operator just like
43 <TT>+</TT> or <TT>-</TT>.
44 C does not have ``assignment statements'';
45 instead,
46 an assignment like <TT>a = b</TT> is an expression
47 and can be used wherever any expression can appear.
48 Since it's an expression,
49 the assignment <TT>a = b</TT> has a value,
50 namely, the same value that's assigned to <TT>a</TT>.
51 This value can then be used in a larger expression;
52 for example,
53 we might write
54 <pre>
55 c = a = b
56 </pre>
57 which is equivalent to
58 <pre>
59 c = (a = b)
60 </pre>
61 and assigns <TT>b</TT>'s value to both <TT>a</TT> and <TT>c</TT>.
62 (The assignment operator, therefore, groups from right to left.)
63 Later we'll see other circumstances
64 in which it can be useful to use the value of an assignment expression.
65 </p><p>It's usually a matter of style whether you initialize a
66 variable with an initializer in its declaration or with an
67 assignment expression near where you first use it.
68 That is,
69 there's no particular difference between
70 <pre>
71 int a = 10;
72 </pre>
73 and
74 <pre>
75 int a;
77 /* later... */
79 a = 10;
80 </pre>
81 </p><hr>
82 <p>
83 Read sequentially:
84 <a href="sx2e.html" rev=precedes>prev</a>
85 <a href="sx2g.html" rel=precedes>next</a>
86 <a href="sx2.html" rev=subdocument>up</a>
87 <a href="top.html">top</a>
88 </p>
89 <p>
90 This page by <a href="http://www.eskimo.com/~scs/">Steve Summit</a>
91 // <a href="copyright.html">Copyright</a> 1995, 1996
92 // <a href="mailto:scs@eskimo.com">mail feedback</a>
93 </p>
94 </body>
95 </html>