source: public/doc/gnu-c/Assignment-Expressions.html@ 02598c2

Last change on this file since 02598c2 was 02598c2, checked in by Mikhail Kirillov <w96k@…>, on Oct 6, 2022 at 12:36:29 PM

Add gnu-c

  • Property mode set to 100644
File size: 7.2 KB
Line 
1<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
2<html>
3<!-- Copyright (C) 2022 Richard Stallman and Free Software Foundation, Inc.
4
5(The work of Trevis Rothwell and Nelson Beebe has been assigned or
6licensed to the FSF.)
7
8Permission is granted to copy, distribute and/or modify this document
9under the terms of the GNU Free Documentation License, Version 1.3 or
10any later version published by the Free Software Foundation; with the
11Invariant Sections being "GNU General Public License," with the
12Front-Cover Texts being "A GNU Manual," and with the Back-Cover
13Texts as in (a) below. A copy of the license is included in the
14section entitled "GNU Free Documentation License."
15
16(a) The FSF's Back-Cover Text is: "You have the freedom to copy and
17modify this GNU manual. Buying copies from the FSF supports it in
18developing GNU and promoting software freedom." -->
19<!-- Created by GNU Texinfo 6.7, http://www.gnu.org/software/texinfo/ -->
20<head>
21<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
22<title>Assignment Expressions (GNU C Language Manual)</title>
23
24<meta name="description" content="Assignment Expressions (GNU C Language Manual)">
25<meta name="keywords" content="Assignment Expressions (GNU C Language Manual)">
26<meta name="resource-type" content="document">
27<meta name="distribution" content="global">
28<meta name="Generator" content="makeinfo">
29<link href="index.html" rel="start" title="Top">
30<link href="Symbol-Index.html" rel="index" title="Symbol Index">
31<link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
32<link href="index.html" rel="up" title="Top">
33<link href="Simple-Assignment.html" rel="next" title="Simple Assignment">
34<link href="Bitwise-Operations.html" rel="prev" title="Bitwise Operations">
35<style type="text/css">
36<!--
37a.summary-letter {text-decoration: none}
38blockquote.indentedblock {margin-right: 0em}
39div.display {margin-left: 3.2em}
40div.example {margin-left: 3.2em}
41div.lisp {margin-left: 3.2em}
42kbd {font-style: oblique}
43pre.display {font-family: inherit}
44pre.format {font-family: inherit}
45pre.menu-comment {font-family: serif}
46pre.menu-preformatted {font-family: serif}
47span.nolinebreak {white-space: nowrap}
48span.roman {font-family: initial; font-weight: normal}
49span.sansserif {font-family: sans-serif; font-weight: normal}
50ul.no-bullet {list-style: none}
51-->
52</style>
53
54
55</head>
56
57<body lang="en">
58<span id="Assignment-Expressions"></span><div class="header">
59<p>
60Next: <a href="Execution-Control-Expressions.html" accesskey="n" rel="next">Execution Control Expressions</a>, Previous: <a href="Arithmetic.html" accesskey="p" rel="prev">Arithmetic</a>, Up: <a href="index.html" accesskey="u" rel="up">Top</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Symbol-Index.html" title="Index" rel="index">Index</a>]</p>
61</div>
62<hr>
63<span id="Assignment-Expressions-1"></span><h2 class="chapter">7 Assignment Expressions</h2>
64<span id="index-assignment-expressions"></span>
65<span id="index-operators_002c-assignment"></span>
66
67<p>As a general concept in programming, an <em>assignment</em> is a
68construct that stores a new value into a place where values can be
69stored&mdash;for instance, in a variable. Such places are called
70<em>lvalues</em> (see <a href="Lvalues.html">Lvalues</a>) because they are locations that hold a value.
71</p>
72<p>An assignment in C is an expression because it has a value; we call
73it an <em>assignment expression</em>. A simple assignment looks like
74</p>
75<div class="example">
76<pre class="example"><var>lvalue</var> = <var>value-to-store</var>
77</pre></div>
78
79<p>We say it assigns the value of the expression <var>value-to-store</var> to
80the location <var>lvalue</var>, or that it stores <var>value-to-store</var>
81there. You can think of the &ldquo;l&rdquo; in &ldquo;lvalue&rdquo; as standing for
82&ldquo;left,&rdquo; since that&rsquo;s what you put on the left side of the assignment
83operator.
84</p>
85<p>However, that&rsquo;s not the only way to use an lvalue, and not all lvalues
86can be assigned to. To use the lvalue in the left side of an
87assignment, it has to be <em>modifiable</em>. In C, that means it was
88not declared with the type qualifier <code>const</code> (see <a href="const.html">const</a>).
89</p>
90<p>The value of the assignment expression is that of <var>lvalue</var> after
91the new value is stored in it. This means you can use an assignment
92inside other expressions. Assignment operators are right-associative
93so that
94</p>
95<div class="example">
96<pre class="example">x = y = z = 0;
97</pre></div>
98
99<p>is equivalent to
100</p>
101<div class="example">
102<pre class="example">x = (y = (z = 0));
103</pre></div>
104
105<p>This is the only useful way for them to associate;
106the other way,
107</p>
108<div class="example">
109<pre class="example">((x = y) = z) = 0;
110</pre></div>
111
112<p>would be invalid since an assignment expression such as <code>x = y</code>
113is not valid as an lvalue.
114</p>
115<p><strong>Warning:</strong> Write parentheses around an assignment if you nest
116it inside another expression, unless that is a conditional expression,
117or comma-separated series, or another assignment.
118</p>
119<table class="menu" border="0" cellspacing="0">
120<tr><td align="left" valign="top">&bull; <a href="Simple-Assignment.html" accesskey="1">Simple Assignment</a></td><td>&nbsp;&nbsp;</td><td align="left" valign="top">The basics of storing a value.
121</td></tr>
122<tr><td align="left" valign="top">&bull; <a href="Lvalues.html" accesskey="2">Lvalues</a></td><td>&nbsp;&nbsp;</td><td align="left" valign="top">Expressions into which a value can be stored.
123</td></tr>
124<tr><td align="left" valign="top">&bull; <a href="Modifying-Assignment.html" accesskey="3">Modifying Assignment</a></td><td>&nbsp;&nbsp;</td><td align="left" valign="top">Shorthand for changing an lvalue&rsquo;s contents.
125</td></tr>
126<tr><td align="left" valign="top">&bull; <a href="Increment_002fDecrement.html" accesskey="4">Increment/Decrement</a></td><td>&nbsp;&nbsp;</td><td align="left" valign="top">Shorthand for incrementing and decrementing
127 an lvalue&rsquo;s contents.
128</td></tr>
129<tr><td align="left" valign="top">&bull; <a href="Postincrement_002fPostdecrement.html" accesskey="5">Postincrement/Postdecrement</a></td><td>&nbsp;&nbsp;</td><td align="left" valign="top">Accessing then incrementing or decrementing.
130</td></tr>
131<tr><td align="left" valign="top">&bull; <a href="Assignment-in-Subexpressions.html" accesskey="6">Assignment in Subexpressions</a></td><td>&nbsp;&nbsp;</td><td align="left" valign="top">How to avoid ambiguity.
132</td></tr>
133<tr><td align="left" valign="top">&bull; <a href="Write-Assignments-Separately.html" accesskey="7">Write Assignments Separately</a></td><td>&nbsp;&nbsp;</td><td align="left" valign="top">Write assignments as separate statements.
134</td></tr>
135</table>
136
137<hr>
138<div class="header">
139<p>
140Next: <a href="Execution-Control-Expressions.html" accesskey="n" rel="next">Execution Control Expressions</a>, Previous: <a href="Arithmetic.html" accesskey="p" rel="prev">Arithmetic</a>, Up: <a href="index.html" accesskey="u" rel="up">Top</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Symbol-Index.html" title="Index" rel="index">Index</a>]</p>
141</div>
142
143
144
145</body>
146</html>
Note: See TracBrowser for help on using the repository browser.