source: public/doc/gnu-c/Blocks.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: 6.0 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>Blocks (GNU C Language Manual)</title>
23
24<meta name="description" content="Blocks (GNU C Language Manual)">
25<meta name="keywords" content="Blocks (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="return-Statement.html" rel="next" title="return Statement">
34<link href="if_002delse-Statement.html" rel="prev" title="if-else Statement">
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="Blocks"></span><div class="header">
59<p>
60Next: <a href="return-Statement.html" accesskey="n" rel="next">return Statement</a>, Previous: <a href="if_002delse-Statement.html" accesskey="p" rel="prev">if-else Statement</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="Blocks-1"></span><h3 class="section">19.4 Blocks</h3>
64<span id="index-block"></span>
65<span id="index-compound-statement"></span>
66
67<p>A <em>block</em> is a construct that contains multiple statements of any
68kind. It begins with &lsquo;<samp>{</samp>&rsquo; and ends with &lsquo;<samp>}</samp>&rsquo;, and has a
69series of statements and declarations in between. Another name for
70blocks is <em>compound statements</em>.
71</p>
72<p>Is a block a statement? Yes and no. It doesn&rsquo;t <em>look</em> like a
73normal statement&mdash;it does not end with a semicolon. But you can
74<em>use</em> it like a statement; anywhere that a statement is required
75or allowed, you can write a block and consider that block a statement.
76</p>
77<p>So far it seems that a block is a kind of statement with an unusual
78syntax. But that is not entirely true: a function body is also a
79block, and that block is definitely not a statement. The text after a
80function header is not treated as a statement; only a function body is
81allowed there, and nothing else would be meaningful there.
82</p>
83<p>In a formal grammar we would have to choose&mdash;either a block is a kind
84of statement or it is not. But this manual is meant for humans, not
85for parser generators. The clearest answer for humans is, &ldquo;a block
86is a statement, in some ways.&rdquo;
87</p>
88<span id="index-nested-block"></span>
89<span id="index-internal-block"></span>
90<p>A block that isn&rsquo;t a function body is called an <em>internal block</em>
91or a <em>nested block</em>. You can put a nested block directly inside
92another block, but more often the nested block is inside some complex
93statement, such as a <code>for</code> statement or an <code>if</code> statement.
94</p>
95<p>There are two uses for nested blocks in C:
96</p>
97<ul>
98<li> To specify the scope for local declarations. For instance, a local
99variable&rsquo;s scope is the rest of the innermost containing block.
100
101</li><li> To write a series of statements where, syntactically, one statement is
102called for. For instance, the <var>execute-if-true</var> of an <code>if</code>
103statement is one statement. To put multiple statements there, they
104have to be wrapped in a block, like this:
105
106<div class="example">
107<pre class="example">if (x &lt; 0)
108 {
109 printf (&quot;x was negative\n&quot;);
110 x = -x;
111 }
112</pre></div>
113</li></ul>
114
115<p>This example (repeated from above) shows a nested block which serves
116both purposes: it includes two statements (plus a declaration) in the
117body of a <code>while</code> statement, and it provides the scope for the
118declaration of <code>q</code>.
119</p>
120<div class="example">
121<pre class="example">void
122free_intlist (struct intlistlink *p)
123{
124 while (p)
125 {
126 struct intlistlink *q = p;
127 p = p-&gt;next;
128 free (q);
129 }
130}
131</pre></div>
132
133<hr>
134<div class="header">
135<p>
136Next: <a href="return-Statement.html" accesskey="n" rel="next">return Statement</a>, Previous: <a href="if_002delse-Statement.html" accesskey="p" rel="prev">if-else Statement</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>
137</div>
138
139
140
141</body>
142</html>
Note: See TracBrowser for help on using the repository browser.