blob: ab3db58331f9ed1f14d890d720b94affa46660ac (
plain)
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
|
<h1 id="firstHeading" class="firstHeading">goto statement</h1> <p>Transfers control unconditionally to the desired location.</p>
<p>Used when it is otherwise impossible to transfer control to the desired location using conventional constructs.</p>
<h3 id="Syntax"> Syntax</h3> <table class="t-sdsc-begin"> <tr class="t-sdsc"> <td class="t-sdsc-nopad"> <span class="t-spar">attr-spec-seq</span><span class="t-mark">(optional)</span> <code>goto</code> <span class="t-spar">label</span> <code>;</code> </td> <td class="t-sdsc-nopad"> </td> <td class="t-sdsc-nopad"> </td>
</tr>
</table> <table class="t-par-begin"> <tr class="t-par"> <td> <span class="t-spar">label</span> </td> <td> - </td> <td> target <a href="statements#Labels" title="c/language/statements">label</a> for the <code>goto</code> statement </td>
</tr> <tr class="t-par"> <td> <span class="t-spar">attr-spec-seq</span> </td> <td> - </td> <td> <span class="t-mark-rev t-since-c23">(C23)</span>optional list of <a href="attributes" title="c/language/attributes">attributes</a>, applied to the <code>goto</code> statement </td>
</tr>
</table> <h3 id="Explanation"> Explanation</h3> <p>The <code>goto</code> statement causes an unconditional jump (transfer of control) to the statement prefixed by the named <span class="t-spar">label</span> (which must appear in the same function as the goto statement)<span class="t-rev-inl t-since-c99"><span>, except when this jump would enter the scope of a <a href="array" title="c/language/array">variable-length array</a> or another <a href="declarations" title="c/language/declarations">variably-modified type</a>.</span><span><span class="t-mark-rev t-since-c99">(since C99)</span></span></span></p>
<p>A <span class="t-spar">label</span> is an identifier followed by a colon (<code>:</code>)<span class="t-rev-inl t-until-c23"><span> and a statement</span><span><span class="t-mark-rev t-until-c23">(until C23)</span></span></span>. Labels are the only identifiers that have <i>function scope</i>: they can be used (in a goto statement) anywhere in the same function in which they appear. There may be multiple labels before any statement.</p>
<table class="t-rev-begin"> <tr class="t-rev t-since-c99">
<td> <p>Entering the scope of a non-variably modified variable is permitted:</p>
<div class="c source-c"><pre data-language="c">goto lab1; // OK: going into the scope of a regular variable
int n = 5;
lab1:; // Note, n is uninitialized, as if declared by int n;
// goto lab2; // Error: going into the scope of two VM types
double a[n]; // a VLA
int (*p)[n]; // a VM pointer
lab2:</pre></div> <p>If <code>goto</code> leaves the scope of a VLA, it is deallocated (and may be reallocated if its initialization is executed again):</p>
<div class="c source-c"><pre data-language="c">{
int n = 1;
label:;
int a[n]; // re-allocated 10 times, each with a different size
if (n++ < 10) goto label; // leaving the scope of a VM
}</pre></div> </td> <td><span class="t-mark-rev t-since-c99">(since C99)</span></td>
</tr> </table> <h3 id="Keywords"> Keywords</h3> <p><a href="../keyword/goto" title="c/keyword/goto"><code>goto</code></a></p>
<h3 id="Notes"> Notes</h3> <table class="t-rev-begin"> <tr class="t-rev t-until-c23">
<td> <p>Because declarations are not statements, a label before a declaration must use a null statement (a semicolon immediately after the colon). Same applies to a label before the end of a block.</p>
</td> <td><span class="t-mark-rev t-until-c23">(until C23)</span></td>
</tr> </table> <p>C++ imposes additional limitations on the <code>goto</code> statement, but allows labels before declarations (which are statements in C++).</p>
<h3 id="Example"> Example</h3> <div class="t-example"> <div class="c source-c"><pre data-language="c">#include <stdio.h>
int main(void)
{
// goto can be used to leave a multi-level loop easily
for (int x = 0; x < 3; x++) {
for (int y = 0; y < 3; y++) {
printf("(%d;%d)\n",x,y);
if (x + y >= 3) goto endloop;
}
}
endloop:;
}</pre></div> <p>Output:</p>
<div class="text source-text"><pre data-language="c">(0;0)
(0;1)
(0;2)
(1;0)
(1;1)
(1;2)</pre></div> </div> <h3 id="References"> References</h3> <ul>
<li> C17 standard (ISO/IEC 9899:2018): </li>
<ul><li> 6.8.6.1 The goto statement (p: 110-111) </li></ul>
<li> C11 standard (ISO/IEC 9899:2011): </li>
<ul><li> 6.8.6.1 The goto statement (p: 152-153) </li></ul>
<li> C99 standard (ISO/IEC 9899:1999): </li>
<ul><li> 6.8.6.1 The goto statement (p: 137-138) </li></ul>
<li> C89/C90 standard (ISO/IEC 9899:1990): </li>
<ul><li> 3.6.6.1 The goto statement </li></ul>
</ul> <h3 id="See_also"> See also</h3> <table class="t-dsc-begin"> <tr class="t-dsc"> <td colspan="2"> <span><a href="https://en.cppreference.com/w/cpp/language/goto" title="cpp/language/goto">C++ documentation</a></span> for <span class=""><span><code>goto</code> statement</span></span> </td>
</tr> </table> <div class="_attribution">
<p class="_attribution-p">
© cppreference.com<br>Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.<br>
<a href="https://en.cppreference.com/w/c/language/goto" class="_attribution-link">https://en.cppreference.com/w/c/language/goto</a>
</p>
</div>
|