summaryrefslogtreecommitdiff
path: root/devdocs/c/language%2Fgoto.html
diff options
context:
space:
mode:
Diffstat (limited to 'devdocs/c/language%2Fgoto.html')
-rw-r--r--devdocs/c/language%2Fgoto.html64
1 files changed, 64 insertions, 0 deletions
diff --git a/devdocs/c/language%2Fgoto.html b/devdocs/c/language%2Fgoto.html
new file mode 100644
index 00000000..ab3db583
--- /dev/null
+++ b/devdocs/c/language%2Fgoto.html
@@ -0,0 +1,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++ &lt; 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 &lt;stdio.h&gt;
+
+int main(void)
+{
+ // goto can be used to leave a multi-level loop easily
+ for (int x = 0; x &lt; 3; x++) {
+ for (int y = 0; y &lt; 3; y++) {
+ printf("(%d;%d)\n",x,y);
+ if (x + y &gt;= 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">
+ &copy; 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>