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
|
---|
6 | licensed to the FSF.)
|
---|
7 |
|
---|
8 | Permission is granted to copy, distribute and/or modify this document
|
---|
9 | under the terms of the GNU Free Documentation License, Version 1.3 or
|
---|
10 | any later version published by the Free Software Foundation; with the
|
---|
11 | Invariant Sections being "GNU General Public License," with the
|
---|
12 | Front-Cover Texts being "A GNU Manual," and with the Back-Cover
|
---|
13 | Texts as in (a) below. A copy of the license is included in the
|
---|
14 | section entitled "GNU Free Documentation License."
|
---|
15 |
|
---|
16 | (a) The FSF's Back-Cover Text is: "You have the freedom to copy and
|
---|
17 | modify this GNU manual. Buying copies from the FSF supports it in
|
---|
18 | developing 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>Statement Exprs (GNU C Language Manual)</title>
|
---|
23 |
|
---|
24 | <meta name="description" content="Statement Exprs (GNU C Language Manual)">
|
---|
25 | <meta name="keywords" content="Statement Exprs (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="Statements.html" rel="up" title="Statements">
|
---|
33 | <link href="Variables.html" rel="next" title="Variables">
|
---|
34 | <link href="Label-Value-Caveats.html" rel="prev" title="Label Value Caveats">
|
---|
35 | <style type="text/css">
|
---|
36 | <!--
|
---|
37 | a.summary-letter {text-decoration: none}
|
---|
38 | blockquote.indentedblock {margin-right: 0em}
|
---|
39 | div.display {margin-left: 3.2em}
|
---|
40 | div.example {margin-left: 3.2em}
|
---|
41 | div.lisp {margin-left: 3.2em}
|
---|
42 | kbd {font-style: oblique}
|
---|
43 | pre.display {font-family: inherit}
|
---|
44 | pre.format {font-family: inherit}
|
---|
45 | pre.menu-comment {font-family: serif}
|
---|
46 | pre.menu-preformatted {font-family: serif}
|
---|
47 | span.nolinebreak {white-space: nowrap}
|
---|
48 | span.roman {font-family: initial; font-weight: normal}
|
---|
49 | span.sansserif {font-family: sans-serif; font-weight: normal}
|
---|
50 | ul.no-bullet {list-style: none}
|
---|
51 | -->
|
---|
52 | </style>
|
---|
53 |
|
---|
54 |
|
---|
55 | </head>
|
---|
56 |
|
---|
57 | <body lang="en">
|
---|
58 | <span id="Statement-Exprs"></span><div class="header">
|
---|
59 | <p>
|
---|
60 | Previous: <a href="Labels-as-Values.html" accesskey="p" rel="prev">Labels as Values</a>, Up: <a href="Statements.html" accesskey="u" rel="up">Statements</a> [<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="Statements-and-Declarations-in-Expressions"></span><h3 class="section">19.15 Statements and Declarations in Expressions</h3>
|
---|
64 | <span id="index-statements-inside-expressions"></span>
|
---|
65 | <span id="index-declarations-inside-expressions"></span>
|
---|
66 | <span id="index-expressions-containing-statements"></span>
|
---|
67 |
|
---|
68 | <p>A block enclosed in parentheses can be used as an expression in GNU
|
---|
69 | C. This provides a way to use local variables, loops and switches within
|
---|
70 | an expression. We call it a <em>statement expression</em>.
|
---|
71 | </p>
|
---|
72 | <p>Recall that a block is a sequence of statements
|
---|
73 | surrounded by braces. In this construct, parentheses go around the
|
---|
74 | braces. For example:
|
---|
75 | </p>
|
---|
76 | <div class="example">
|
---|
77 | <pre class="example">({ int y = foo (); int z;
|
---|
78 | if (y > 0) z = y;
|
---|
79 | else z = - y;
|
---|
80 | z; })
|
---|
81 | </pre></div>
|
---|
82 |
|
---|
83 | <p>is a valid (though slightly more complex than necessary) expression
|
---|
84 | for the absolute value of <code>foo ()</code>.
|
---|
85 | </p>
|
---|
86 | <p>The last statement in the block should be an expression statement; an
|
---|
87 | expression followed by a semicolon, that is. The value of this
|
---|
88 | expression serves as the value of statement expression. If the last
|
---|
89 | statement is anything else, the statement expression’s value is
|
---|
90 | <code>void</code>.
|
---|
91 | </p>
|
---|
92 | <p>This feature is mainly useful in making macro definitions compute each
|
---|
93 | operand exactly once. See <a href="Macros-and-Auto-Type.html">Macros and Auto Type</a>.
|
---|
94 | </p>
|
---|
95 | <p>Statement expressions are not allowed in expressions that must be
|
---|
96 | constant, such as the value for an enumerator, the width of a
|
---|
97 | bit-field, or the initial value of a static variable.
|
---|
98 | </p>
|
---|
99 | <p>Jumping into a statement expression—with <code>goto</code>, or using a
|
---|
100 | <code>switch</code> statement outside the statement expression—is an
|
---|
101 | error. With a computed <code>goto</code> (see <a href="Labels-as-Values.html">Labels as Values</a>), the
|
---|
102 | compiler can’t detect the error, but it still won’t work.
|
---|
103 | </p>
|
---|
104 | <p>Jumping out of a statement expression is permitted, but since
|
---|
105 | subexpressions in C are not computed in a strict order, it is
|
---|
106 | unpredictable which other subexpressions will have been computed by
|
---|
107 | then. For example,
|
---|
108 | </p>
|
---|
109 | <div class="example">
|
---|
110 | <pre class="example"> foo (), (({ bar1 (); goto a; 0; }) + bar2 ()), baz();
|
---|
111 | </pre></div>
|
---|
112 |
|
---|
113 | <p>calls <code>foo</code> and <code>bar1</code> before it jumps, and never
|
---|
114 | calls <code>baz</code>, but may or may not call <code>bar2</code>. If <code>bar2</code>
|
---|
115 | does get called, that occurs after <code>foo</code> and before <code>bar1</code>.
|
---|
116 | </p>
|
---|
117 | <hr>
|
---|
118 | <div class="header">
|
---|
119 | <p>
|
---|
120 | Previous: <a href="Labels-as-Values.html" accesskey="p" rel="prev">Labels as Values</a>, Up: <a href="Statements.html" accesskey="u" rel="up">Statements</a> [<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>
|
---|
121 | </div>
|
---|
122 |
|
---|
123 |
|
---|
124 |
|
---|
125 | </body>
|
---|
126 | </html>
|
---|