source: public/doc/gnu-c/goto-Statement.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: 7.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>goto Statement (GNU C Language Manual)</title>
23
24<meta name="description" content="goto Statement (GNU C Language Manual)">
25<meta name="keywords" content="goto Statement (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="Local-Labels.html" rel="next" title="Local Labels">
34<link href="Null-Statement.html" rel="prev" title="Null 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="goto-Statement"></span><div class="header">
59<p>
60Next: <a href="Local-Labels.html" accesskey="n" rel="next">Local Labels</a>, Previous: <a href="Null-Statement.html" accesskey="p" rel="prev">Null 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="goto-Statement-and-Labels"></span><h3 class="section">19.12 <code>goto</code> Statement and Labels</h3>
64<span id="index-goto-statement"></span>
65<span id="index-statement_002c-goto"></span>
66<span id="index-label"></span>
67<span id="index-goto"></span>
68
69<p>The <code>goto</code> statement looks like this:
70</p>
71<div class="example">
72<pre class="example">goto <var>label</var>;
73</pre></div>
74
75<p>Its effect is to transfer control immediately to another part of the
76current function&mdash;where the label named <var>label</var> is defined.
77</p>
78<p>An ordinary label definition looks like this:
79</p>
80<div class="example">
81<pre class="example"><var>label</var>:
82</pre></div>
83
84<p>and it can appear before any statement. You can&rsquo;t use <code>default</code>
85as a label, since that has a special meaning for <code>switch</code>
86statements.
87</p>
88<p>An ordinary label doesn&rsquo;t need a separate declaration; defining it is
89enough.
90</p>
91<p>Here&rsquo;s an example of using <code>goto</code> to implement a loop
92equivalent to <code>do</code>&ndash;<code>while</code>:
93</p>
94<div class="example">
95<pre class="example">{
96 loop_restart:
97 <var>body</var>
98 if (<var>condition</var>)
99 goto loop_restart;
100}
101</pre></div>
102
103<p>The name space of labels is separate from that of variables and functions.
104Thus, there is no error in using a single name in both ways:
105</p>
106<div class="example">
107<pre class="example">{
108 int foo; // <span class="roman">Variable <code>foo</code>.</span>
109 foo: // <span class="roman">Label <code>foo</code>.</span>
110 <var>body</var>
111 if (foo &gt; 0) // <span class="roman">Variable <code>foo</code>.</span>
112 goto foo; // <span class="roman">Label <code>foo</code>.</span>
113}
114</pre></div>
115
116<p>Blocks have no effect on ordinary labels; each label name is defined
117throughout the whole of the function it appears in. It looks strange to
118jump into a block with <code>goto</code>, but it works. For example,
119</p>
120<div class="example">
121<pre class="example">if (x &lt; 0)
122 goto negative;
123if (y &lt; 0)
124 {
125 negative:
126 printf (&quot;Negative\n&quot;);
127 return;
128 }
129</pre></div>
130
131<p>If the goto jumps into the scope of a variable, it does not
132initialize the variable. For example, if <code>x</code> is negative,
133</p>
134<div class="example">
135<pre class="example">if (x &lt; 0)
136 goto negative;
137if (y &lt; 0)
138 {
139 int i = 5;
140 negative:
141 printf (&quot;Negative, and i is %d\n&quot;, i);
142 return;
143 }
144</pre></div>
145
146<p>prints junk because <code>i</code> was not initialized.
147</p>
148<p>If the block declares a variable-length automatic array, jumping into
149it gives a compilation error. However, jumping out of the scope of a
150variable-length array works fine, and deallocates its storage.
151</p>
152<p>A label can&rsquo;t come directly before a declaration, so the code can&rsquo;t
153jump directly to one. For example, this is not allowed:
154</p>
155<div class="example">
156<pre class="example">{
157 goto foo;
158foo:
159 int x = 5;
160 bar(&amp;x);
161}
162</pre></div>
163
164<p>The workaround is to add a statement, even an empty statement,
165directly after the label. For example:
166</p>
167<div class="example">
168<pre class="example">{
169 goto foo;
170foo:
171 ;
172 int x = 5;
173 bar(&amp;x);
174}
175</pre></div>
176
177<p>Likewise, a label can&rsquo;t be the last thing in a block. The workaround
178solution is the same: add a semicolon after the label.
179</p>
180<p>These unnecessary restrictions on labels make no sense, and ought in
181principle to be removed; but they do only a little harm since labels
182and <code>goto</code> are rarely the best way to write a program.
183</p>
184<p>These examples are all artificial; it would be more natural to
185write them in other ways, without <code>goto</code>. For instance,
186the clean way to write the example that prints &lsquo;<samp>Negative</samp>&rsquo; is this:
187</p>
188<div class="example">
189<pre class="example">if (x &lt; 0 || y &lt; 0)
190 {
191 printf (&quot;Negative\n&quot;);
192 return;
193 }
194</pre></div>
195
196<p>It is hard to construct simple examples where <code>goto</code> is actually
197the best way to write a program. Its rare good uses tend to be in
198complex code, thus not apt for the purpose of explaining the meaning
199of <code>goto</code>.
200</p>
201<p>The only good time to use <code>goto</code> is when it makes the code
202simpler than any alternative. Jumping backward is rarely desirable,
203because usually the other looping and control constructs give simpler
204code. Using <code>goto</code> to jump forward is more often desirable, for
205instance when a function needs to do some processing in an error case
206and errors can occur at various different places within the function.
207</p>
208<hr>
209<div class="header">
210<p>
211Next: <a href="Local-Labels.html" accesskey="n" rel="next">Local Labels</a>, Previous: <a href="Null-Statement.html" accesskey="p" rel="prev">Null 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>
212</div>
213
214
215
216</body>
217</html>
Note: See TracBrowser for help on using the repository browser.