From 754bbf7a25a8dda49b5d08ef0d0443bbf5af0e36 Mon Sep 17 00:00:00 2001 From: Craig Jennings Date: Sun, 7 Apr 2024 13:41:34 -0500 Subject: new repository --- devdocs/c/language%2Fgoto.html | 64 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 devdocs/c/language%2Fgoto.html (limited to 'devdocs/c/language%2Fgoto.html') 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 @@ +

goto statement

Transfers control unconditionally to the desired location.

+

Used when it is otherwise impossible to transfer control to the desired location using conventional constructs.

+

Syntax

+ +
attr-spec-seq(optional) goto label ;
+ + +
label - target label for the goto statement
attr-spec-seq - (C23)optional list of attributes, applied to the goto statement

Explanation

The goto statement causes an unconditional jump (transfer of control) to the statement prefixed by the named label (which must appear in the same function as the goto statement), except when this jump would enter the scope of a variable-length array or another variably-modified type.(since C99)

+

A label is an identifier followed by a colon (:) and a statement(until C23). Labels are the only identifiers that have function scope: 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.

+ + +

Entering the scope of a non-variably modified variable is permitted:

+
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:

If goto leaves the scope of a VLA, it is deallocated (and may be reallocated if its initialization is executed again):

+
{
+   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
+}
(since C99)

Keywords

goto

+

Notes

+ +

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.

+
(until C23)

C++ imposes additional limitations on the goto statement, but allows labels before declarations (which are statements in C++).

+

Example

#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:;
+}

Output:

+
(0;0)
+(0;1)
+(0;2)
+(1;0)
+(1;1)
+(1;2)

References

See also

+
C++ documentation for goto statement
+

+ © cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
+ https://en.cppreference.com/w/c/language/goto +

+
-- cgit v1.2.3