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>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 | <!--
|
---|
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="Blocks"></span><div class="header">
|
---|
59 | <p>
|
---|
60 | Next: <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> [<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
|
---|
68 | kind. It begins with ‘<samp>{</samp>’ and ends with ‘<samp>}</samp>’, and has a
|
---|
69 | series of statements and declarations in between. Another name for
|
---|
70 | blocks is <em>compound statements</em>.
|
---|
71 | </p>
|
---|
72 | <p>Is a block a statement? Yes and no. It doesn’t <em>look</em> like a
|
---|
73 | normal statement—it does not end with a semicolon. But you can
|
---|
74 | <em>use</em> it like a statement; anywhere that a statement is required
|
---|
75 | or 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
|
---|
78 | syntax. But that is not entirely true: a function body is also a
|
---|
79 | block, and that block is definitely not a statement. The text after a
|
---|
80 | function header is not treated as a statement; only a function body is
|
---|
81 | allowed there, and nothing else would be meaningful there.
|
---|
82 | </p>
|
---|
83 | <p>In a formal grammar we would have to choose—either a block is a kind
|
---|
84 | of statement or it is not. But this manual is meant for humans, not
|
---|
85 | for parser generators. The clearest answer for humans is, “a block
|
---|
86 | is a statement, in some ways.”
|
---|
87 | </p>
|
---|
88 | <span id="index-nested-block"></span>
|
---|
89 | <span id="index-internal-block"></span>
|
---|
90 | <p>A block that isn’t a function body is called an <em>internal block</em>
|
---|
91 | or a <em>nested block</em>. You can put a nested block directly inside
|
---|
92 | another block, but more often the nested block is inside some complex
|
---|
93 | statement, 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
|
---|
99 | variable’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
|
---|
102 | called for. For instance, the <var>execute-if-true</var> of an <code>if</code>
|
---|
103 | statement is one statement. To put multiple statements there, they
|
---|
104 | have to be wrapped in a block, like this:
|
---|
105 |
|
---|
106 | <div class="example">
|
---|
107 | <pre class="example">if (x < 0)
|
---|
108 | {
|
---|
109 | printf ("x was negative\n");
|
---|
110 | x = -x;
|
---|
111 | }
|
---|
112 | </pre></div>
|
---|
113 | </li></ul>
|
---|
114 |
|
---|
115 | <p>This example (repeated from above) shows a nested block which serves
|
---|
116 | both purposes: it includes two statements (plus a declaration) in the
|
---|
117 | body of a <code>while</code> statement, and it provides the scope for the
|
---|
118 | declaration of <code>q</code>.
|
---|
119 | </p>
|
---|
120 | <div class="example">
|
---|
121 | <pre class="example">void
|
---|
122 | free_intlist (struct intlistlink *p)
|
---|
123 | {
|
---|
124 | while (p)
|
---|
125 | {
|
---|
126 | struct intlistlink *q = p;
|
---|
127 | p = p->next;
|
---|
128 | free (q);
|
---|
129 | }
|
---|
130 | }
|
---|
131 | </pre></div>
|
---|
132 |
|
---|
133 | <hr>
|
---|
134 | <div class="header">
|
---|
135 | <p>
|
---|
136 | Next: <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> [<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>
|
---|