source: public/doc/gnu-c/Statement-Exprs.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: 5.5 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>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<!--
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="Statement-Exprs"></span><div class="header">
59<p>
60Previous: <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> &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="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
69C. This provides a way to use local variables, loops and switches within
70an expression. We call it a <em>statement expression</em>.
71</p>
72<p>Recall that a block is a sequence of statements
73surrounded by braces. In this construct, parentheses go around the
74braces. For example:
75</p>
76<div class="example">
77<pre class="example">({ int y = foo (); int z;
78 if (y &gt; 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
84for the absolute value of <code>foo ()</code>.
85</p>
86<p>The last statement in the block should be an expression statement; an
87expression followed by a semicolon, that is. The value of this
88expression serves as the value of statement expression. If the last
89statement is anything else, the statement expression&rsquo;s value is
90<code>void</code>.
91</p>
92<p>This feature is mainly useful in making macro definitions compute each
93operand 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
96constant, such as the value for an enumerator, the width of a
97bit-field, or the initial value of a static variable.
98</p>
99<p>Jumping into a statement expression&mdash;with <code>goto</code>, or using a
100<code>switch</code> statement outside the statement expression&mdash;is an
101error. With a computed <code>goto</code> (see <a href="Labels-as-Values.html">Labels as Values</a>), the
102compiler can&rsquo;t detect the error, but it still won&rsquo;t work.
103</p>
104<p>Jumping out of a statement expression is permitted, but since
105subexpressions in C are not computed in a strict order, it is
106unpredictable which other subexpressions will have been computed by
107then. 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
114calls <code>baz</code>, but may or may not call <code>bar2</code>. If <code>bar2</code>
115does get called, that occurs after <code>foo</code> and before <code>bar1</code>.
116</p>
117<hr>
118<div class="header">
119<p>
120Previous: <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> &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>
121</div>
122
123
124
125</body>
126</html>
Note: See TracBrowser for help on using the repository browser.