Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      goto statement

      From cppreference.com
      <c‎ |language
       
       
       
      Statements
      Labels
      Expression statements
      Compound statements
      Selection statements
      Iteration statements
      Jump statements
      goto
       

      Transfers control unconditionally to the desired location.

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

      Contents

      [edit]Syntax

      attr-spec-seq(optional)gotolabel;
      label - targetlabel for thegoto statement
      attr-spec-seq -(C23)optional list ofattributes, applied to thegoto statement

      [edit]Explanation

      Thegoto statement causes an unconditional jump (transfer of control) to the statement prefixed by the namedlabel (which must appear in the same function as the goto statement), except when this jump would enter the scope of avariable-length array or anothervariably-modified type.(since C99)

      Alabel is an identifier followed by a colon (:) and a statement(until C23). Labels are the only identifiers that havefunction 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 variableint n=5;lab1:;// Note, n is uninitialized, as if declared by int n; //   goto lab2;   // Error: going into the scope of two VM typesdouble a[n];// a VLAint(*p)[n];// a VM pointerlab2:

      Ifgoto 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 sizeif(n++<10)goto label;// leaving the scope of a VM}
      (since C99)

      [edit]Keywords

      goto

      [edit]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 thegoto statement, but allows labels before declarations (which are statements in C++).

      [edit]Example

      Run this code
      #include <stdio.h> int main(void){// goto can be used to leave a multi-level loop easilyfor(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)

      [edit]References

      • C17 standard (ISO/IEC 9899:2018):
      • 6.8.6.1 The goto statement (p: 110-111)
      • C11 standard (ISO/IEC 9899:2011):
      • 6.8.6.1 The goto statement (p: 152-153)
      • C99 standard (ISO/IEC 9899:1999):
      • 6.8.6.1 The goto statement (p: 137-138)
      • C89/C90 standard (ISO/IEC 9899:1990):
      • 3.6.6.1 The goto statement

      [edit]See also

      C++ documentation forgoto statement
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=c/language/goto&oldid=130664"

      [8]ページ先頭

      ©2009-2025 Movatter.jp