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>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
>
15 <H2>2.6 Assignment Operators
</H2>
17 <p>[This section corresponds to K
&R Sec.
2.10]
18 </p><p>The assignment operator
<TT>=
</TT> assigns a value to a variable.
28 sets
<TT>a
</TT> to whatever
<TT>b
</TT>'s value is.
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
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'';
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;
57 which is equivalent to
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.
69 there's no particular difference between
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>
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>