1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
<div class="section-level-extent" id="Local-Labels"> <div class="nav-panel"> <p> Next: <a href="labels-as-values" accesskey="n" rel="next">Labels as Values</a>, Previous: <a href="statement-exprs" accesskey="p" rel="prev">Statements and Declarations in Expressions</a>, Up: <a href="c-extensions" accesskey="u" rel="up">Extensions to the C Language Family</a> [<a href="index#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="indices" title="Index" rel="index">Index</a>]</p> </div> <h1 class="section" id="Locally-Declared-Labels"><span>6.2 Locally Declared Labels<a class="copiable-link" href="#Locally-Declared-Labels"> ¶</a></span></h1> <p>GCC allows you to declare <em class="dfn">local labels</em> in any nested block scope. A local label is just like an ordinary label, but you can only reference it (with a <code class="code">goto</code> statement, or by taking its address) within the block in which it is declared. </p> <p>A local label declaration looks like this: </p> <div class="example smallexample"> <pre class="example-preformatted" data-language="cpp">__label__ <var class="var">label</var>;</pre>
</div> <p>or </p> <div class="example smallexample"> <pre class="example-preformatted" data-language="cpp">__label__ <var class="var">label1</var>, <var class="var">label2</var>, /* <span class="r">…</span> */;</pre>
</div> <p>Local label declarations must come at the beginning of the block, before any ordinary declarations or statements. </p> <p>The label declaration defines the label <em class="emph">name</em>, but does not define the label itself. You must do this in the usual way, with <code class="code"><var class="var">label</var>:</code>, within the statements of the statement expression. </p> <p>The local label feature is useful for complex macros. If a macro contains nested loops, a <code class="code">goto</code> can be useful for breaking out of them. However, an ordinary label whose scope is the whole function cannot be used: if the macro can be expanded several times in one function, the label is multiply defined in that function. A local label avoids this problem. For example: </p> <div class="example smallexample"> <pre class="example-preformatted" data-language="cpp">#define SEARCH(value, array, target) \
do { \
__label__ found; \
typeof (target) _SEARCH_target = (target); \
typeof (*(array)) *_SEARCH_array = (array); \
int i, j; \
int value; \
for (i = 0; i < max; i++) \
for (j = 0; j < max; j++) \
if (_SEARCH_array[i][j] == _SEARCH_target) \
{ (value) = i; goto found; } \
(value) = -1; \
found:; \
} while (0)</pre>
</div> <p>This could also be written using a statement expression: </p> <div class="example smallexample"> <pre class="example-preformatted" data-language="cpp">#define SEARCH(array, target) \
({ \
__label__ found; \
typeof (target) _SEARCH_target = (target); \
typeof (*(array)) *_SEARCH_array = (array); \
int i, j; \
int value; \
for (i = 0; i < max; i++) \
for (j = 0; j < max; j++) \
if (_SEARCH_array[i][j] == _SEARCH_target) \
{ value = i; goto found; } \
value = -1; \
found: \
value; \
})</pre>
</div> <p>Local label declarations also make the labels they declare visible to nested functions, if there are any. See <a class="xref" href="nested-functions">Nested Functions</a>, for details. </p> </div> <div class="nav-panel"> <p> Next: <a href="labels-as-values">Labels as Values</a>, Previous: <a href="statement-exprs">Statements and Declarations in Expressions</a>, Up: <a href="c-extensions">Extensions to the C Language Family</a> [<a href="index#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="indices" title="Index" rel="index">Index</a>]</p> </div><div class="_attribution">
<p class="_attribution-p">
© Free Software Foundation<br>Licensed under the GNU Free Documentation License, Version 1.3.<br>
<a href="https://gcc.gnu.org/onlinedocs/gcc-13.1.0/gcc/Local-Labels.html" class="_attribution-link">https://gcc.gnu.org/onlinedocs/gcc-13.1.0/gcc/Local-Labels.html</a>
</p>
</div>
|