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>Local Labels (GNU C Language Manual)</title>
|
---|
23 |
|
---|
24 | <meta name="description" content="Local Labels (GNU C Language Manual)">
|
---|
25 | <meta name="keywords" content="Local Labels (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="Labels-as-Values.html" rel="next" title="Labels as Values">
|
---|
34 | <link href="goto-Statement.html" rel="prev" title="goto 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="Local-Labels"></span><div class="header">
|
---|
59 | <p>
|
---|
60 | Next: <a href="Labels-as-Values.html" accesskey="n" rel="next">Labels as Values</a>, Previous: <a href="goto-Statement.html" accesskey="p" rel="prev">goto 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="Locally-Declared-Labels"></span><h3 class="section">19.13 Locally Declared Labels</h3>
|
---|
64 | <span id="index-local-labels"></span>
|
---|
65 | <span id="index-macros_002c-local-labels"></span>
|
---|
66 | <span id="index-_005f_005flabel_005f_005f"></span>
|
---|
67 |
|
---|
68 | <p>In GNU C you can declare <em>local labels</em> in any nested block
|
---|
69 | scope. A local label is used in a <code>goto</code> statement just like an
|
---|
70 | ordinary label, but you can only reference it within the block in
|
---|
71 | which it was declared.
|
---|
72 | </p>
|
---|
73 | <p>A local label declaration looks like this:
|
---|
74 | </p>
|
---|
75 | <div class="example">
|
---|
76 | <pre class="example">__label__ <var>label</var>;
|
---|
77 | </pre></div>
|
---|
78 |
|
---|
79 | <p>or
|
---|
80 | </p>
|
---|
81 | <div class="example">
|
---|
82 | <pre class="example">__label__ <var>label1</var>, <var>label2</var>, <span class="roman">…</span>;
|
---|
83 | </pre></div>
|
---|
84 |
|
---|
85 | <p>Local label declarations must come at the beginning of the block,
|
---|
86 | before any ordinary declarations or statements.
|
---|
87 | </p>
|
---|
88 | <p>The label declaration declares the label <em>name</em>, but does not define
|
---|
89 | the label itself. That’s done in the usual way, with
|
---|
90 | <code><var>label</var>:</code>, before one of the statements in the block.
|
---|
91 | </p>
|
---|
92 | <p>The local label feature is useful for complex macros. If a macro
|
---|
93 | contains nested loops, a <code>goto</code> can be useful for breaking out of
|
---|
94 | them. However, an ordinary label whose scope is the whole function
|
---|
95 | cannot be used: if the macro can be expanded several times in one
|
---|
96 | function, the label will be multiply defined in that function. A
|
---|
97 | local label avoids this problem. For example:
|
---|
98 | </p>
|
---|
99 | <div class="example">
|
---|
100 | <pre class="example">#define SEARCH(value, array, target) \
|
---|
101 | do { \
|
---|
102 | __label__ found; \
|
---|
103 | __auto_type _SEARCH_target = (target); \
|
---|
104 | __auto_type _SEARCH_array = (array); \
|
---|
105 | int i, j; \
|
---|
106 | int value; \
|
---|
107 | for (i = 0; i < max; i++) \
|
---|
108 | for (j = 0; j < max; j++) \
|
---|
109 | if (_SEARCH_array[i][j] == _SEARCH_target) \
|
---|
110 | { (value) = i; goto found; } \
|
---|
111 | (value) = -1; \
|
---|
112 | found:; \
|
---|
113 | } while (0)
|
---|
114 | </pre></div>
|
---|
115 |
|
---|
116 | <p>This could also be written using a statement expression
|
---|
117 | (see <a href="Statement-Exprs.html">Statement Exprs</a>):
|
---|
118 | </p>
|
---|
119 | <div class="example">
|
---|
120 | <pre class="example">#define SEARCH(array, target) \
|
---|
121 | ({ \
|
---|
122 | __label__ found; \
|
---|
123 | __auto_type _SEARCH_target = (target); \
|
---|
124 | __auto_type _SEARCH_array = (array); \
|
---|
125 | int i, j; \
|
---|
126 | int value; \
|
---|
127 | for (i = 0; i < max; i++) \
|
---|
128 | for (j = 0; j < max; j++) \
|
---|
129 | if (_SEARCH_array[i][j] == _SEARCH_target) \
|
---|
130 | { value = i; goto found; } \
|
---|
131 | value = -1; \
|
---|
132 | found: \
|
---|
133 | value; \
|
---|
134 | })
|
---|
135 | </pre></div>
|
---|
136 |
|
---|
137 | <p>Ordinary labels are visible throughout the function where they are
|
---|
138 | defined, and only in that function. However, explicitly declared
|
---|
139 | local labels of a block are visible in nested functions declared
|
---|
140 | within that block. See <a href="Nested-Functions.html">Nested Functions</a>, for details.
|
---|
141 | </p>
|
---|
142 | <p>See <a href="goto-Statement.html">goto Statement</a>.
|
---|
143 | </p>
|
---|
144 | <hr>
|
---|
145 | <div class="header">
|
---|
146 | <p>
|
---|
147 | Next: <a href="Labels-as-Values.html" accesskey="n" rel="next">Labels as Values</a>, Previous: <a href="goto-Statement.html" accesskey="p" rel="prev">goto 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>
|
---|
148 | </div>
|
---|
149 |
|
---|
150 |
|
---|
151 |
|
---|
152 | </body>
|
---|
153 | </html>
|
---|