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>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 | <!--
|
---|
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="goto-Statement"></span><div class="header">
|
---|
59 | <p>
|
---|
60 | Next: <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> [<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
|
---|
76 | current function—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’t use <code>default</code>
|
---|
85 | as a label, since that has a special meaning for <code>switch</code>
|
---|
86 | statements.
|
---|
87 | </p>
|
---|
88 | <p>An ordinary label doesn’t need a separate declaration; defining it is
|
---|
89 | enough.
|
---|
90 | </p>
|
---|
91 | <p>Here’s an example of using <code>goto</code> to implement a loop
|
---|
92 | equivalent to <code>do</code>–<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.
|
---|
104 | Thus, 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 > 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
|
---|
117 | throughout the whole of the function it appears in. It looks strange to
|
---|
118 | jump into a block with <code>goto</code>, but it works. For example,
|
---|
119 | </p>
|
---|
120 | <div class="example">
|
---|
121 | <pre class="example">if (x < 0)
|
---|
122 | goto negative;
|
---|
123 | if (y < 0)
|
---|
124 | {
|
---|
125 | negative:
|
---|
126 | printf ("Negative\n");
|
---|
127 | return;
|
---|
128 | }
|
---|
129 | </pre></div>
|
---|
130 |
|
---|
131 | <p>If the goto jumps into the scope of a variable, it does not
|
---|
132 | initialize the variable. For example, if <code>x</code> is negative,
|
---|
133 | </p>
|
---|
134 | <div class="example">
|
---|
135 | <pre class="example">if (x < 0)
|
---|
136 | goto negative;
|
---|
137 | if (y < 0)
|
---|
138 | {
|
---|
139 | int i = 5;
|
---|
140 | negative:
|
---|
141 | printf ("Negative, and i is %d\n", 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
|
---|
149 | it gives a compilation error. However, jumping out of the scope of a
|
---|
150 | variable-length array works fine, and deallocates its storage.
|
---|
151 | </p>
|
---|
152 | <p>A label can’t come directly before a declaration, so the code can’t
|
---|
153 | jump directly to one. For example, this is not allowed:
|
---|
154 | </p>
|
---|
155 | <div class="example">
|
---|
156 | <pre class="example">{
|
---|
157 | goto foo;
|
---|
158 | foo:
|
---|
159 | int x = 5;
|
---|
160 | bar(&x);
|
---|
161 | }
|
---|
162 | </pre></div>
|
---|
163 |
|
---|
164 | <p>The workaround is to add a statement, even an empty statement,
|
---|
165 | directly after the label. For example:
|
---|
166 | </p>
|
---|
167 | <div class="example">
|
---|
168 | <pre class="example">{
|
---|
169 | goto foo;
|
---|
170 | foo:
|
---|
171 | ;
|
---|
172 | int x = 5;
|
---|
173 | bar(&x);
|
---|
174 | }
|
---|
175 | </pre></div>
|
---|
176 |
|
---|
177 | <p>Likewise, a label can’t be the last thing in a block. The workaround
|
---|
178 | solution is the same: add a semicolon after the label.
|
---|
179 | </p>
|
---|
180 | <p>These unnecessary restrictions on labels make no sense, and ought in
|
---|
181 | principle to be removed; but they do only a little harm since labels
|
---|
182 | and <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
|
---|
185 | write them in other ways, without <code>goto</code>. For instance,
|
---|
186 | the clean way to write the example that prints ‘<samp>Negative</samp>’ is this:
|
---|
187 | </p>
|
---|
188 | <div class="example">
|
---|
189 | <pre class="example">if (x < 0 || y < 0)
|
---|
190 | {
|
---|
191 | printf ("Negative\n");
|
---|
192 | return;
|
---|
193 | }
|
---|
194 | </pre></div>
|
---|
195 |
|
---|
196 | <p>It is hard to construct simple examples where <code>goto</code> is actually
|
---|
197 | the best way to write a program. Its rare good uses tend to be in
|
---|
198 | complex code, thus not apt for the purpose of explaining the meaning
|
---|
199 | of <code>goto</code>.
|
---|
200 | </p>
|
---|
201 | <p>The only good time to use <code>goto</code> is when it makes the code
|
---|
202 | simpler than any alternative. Jumping backward is rarely desirable,
|
---|
203 | because usually the other looping and control constructs give simpler
|
---|
204 | code. Using <code>goto</code> to jump forward is more often desirable, for
|
---|
205 | instance when a function needs to do some processing in an error case
|
---|
206 | and errors can occur at various different places within the function.
|
---|
207 | </p>
|
---|
208 | <hr>
|
---|
209 | <div class="header">
|
---|
210 | <p>
|
---|
211 | Next: <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> [<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>
|
---|