source: public/doc/gnu-c/Local-Labels.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.7 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>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<!--
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="Local-Labels"></span><div class="header">
59<p>
60Next: <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> &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="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
69scope. A local label is used in a <code>goto</code> statement just like an
70ordinary label, but you can only reference it within the block in
71which 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">&hellip;</span>;
83</pre></div>
84
85<p>Local label declarations must come at the beginning of the block,
86before any ordinary declarations or statements.
87</p>
88<p>The label declaration declares the label <em>name</em>, but does not define
89the label itself. That&rsquo;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
93contains nested loops, a <code>goto</code> can be useful for breaking out of
94them. However, an ordinary label whose scope is the whole function
95cannot be used: if the macro can be expanded several times in one
96function, the label will be multiply defined in that function. A
97local label avoids this problem. For example:
98</p>
99<div class="example">
100<pre class="example">#define SEARCH(value, array, target) \
101do { \
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 &lt; max; i++) \
108 for (j = 0; j &lt; 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 &lt; max; i++) \
128 for (j = 0; j &lt; 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
138defined, and only in that function. However, explicitly declared
139local labels of a block are visible in nested functions declared
140within 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>
147Next: <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> &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>
148</div>
149
150
151
152</body>
153</html>
Note: See TracBrowser for help on using the repository browser.