非常教程

C参考手册

C 语法

goto statement

无条件地将控制权转移到所需位置。

否则无法使用传统构造将控制权转移到所需位置时使用。

句法

goto label ;

label : statement

说明

goto语句导致无条件跳转(控制权转移)到由指定标签(必须出现在与 goto 语句相同的函数中)前面的语句中,除非该跳转将进入可变长度数组的范围或另一变量修改类型。(自 C99以来)。

标签是一个标识符,后跟一个冒号(:)和一个语句。标签是唯一具有函数作用域的标识符:可以在它们出现的相同函数中的任何位置使用它们(在 goto 语句中)。任何陈述之前可能有多个标签。

输入非变化修改变量的范围是允许的:goto lab1; // OK:进入常规变量的范围int n = 5; lab1中的:; //注意,n是未初始化的,就像由int n声明的那样; // goto lab2; //错误:进入两个VM类型的范围double an; //一个VLA int(* p)n; //虚拟机指针lab2:如果goto离开了VLA的范围,它将被释放(并且如果它的初始化被再次执行,可以被重新分配):{int n = 1; 标签:; int an; //重新分配10次,每个都有不同的大小if(n ++ <10)goto label; //留下VM的范围}

(自C99以来)

关键词

goto.

笔记

由于声明不是语句,声明之前的标签必须使用空语句(紧跟在冒号后面的分号)。同样适用于块结束之前的标签。

C ++对goto语句施加了额外的限制,但在声明之前允许标签(这是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:;
}

输出:

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

参考

  • 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

C 语法相关

1.#define directive
2.#elif directive
3.#else directive
4.#endif directive
5.#error directive
6.#if directive
7.#ifdef directive
8.#ifndef directive
9.#include directive
10.#line directive
11.#pragma directive
12.alignas
13.Alternative operators and tokens
14.Analyzability
15.Arithmetic operators
16.Arithmetic types
17.Array declaration
18.Array initialization
19.ASCII Chart
20.Assignment operators
21. types
22.Basic concepts
23.Bit fields
24.break statement
25.C language
26.C Operator Precedence
27.cast operator
28.character constant
29.Comments
30.Comparison operators
31.compound literals
32.Conditional inclusion
33.Conformance
34.const type qualifier
35.Constant expressions
36.continue statement
37.Declarations
38.do-while loop
39.Enumerations
40.Escape sequences
41.Expressions
42.External and tentative definitions
43.File scope
44.floating constant
45.for loop
46.Function declarations
47.Function definitions
48.Functions
49.Generic selection
50.Identifier
51.if statement
52.Implicit conversions
53.Increment/decrement operators
54.Initialization
55.inline function specifier
56.integer constant
57.Lifetime
58.Logical operators
59.Lookup and name spaces
60.Main function
61.Member access operators
62.Memory model
63.Objects and alignment
64.Order of evaluation
65.Other operators
66.Phases of translation
67.Pointer declaration
68.Preprocessor
69.restrict type qualifier
70.return statement
71.Scalar initialization
72.Scope
73.sizeof operator
74.Statements
75.static assert declaration
76.Static storage duration
77.Storage-class specifiers
78.string literals
79.Struct and union initialization
80.Struct declaration
81.switch statement
82.Thread storage duration
83.Type
84.Type
85.Typedef declaration
86.Undefined behavior
87.Union declaration
88.Value categories
89.Variadic arguments
90.volatile type qualifier
91.while loop
92._Alignof operator
93._Noreturn function specifier
C

C 语言是一门通用计算机编程语言,应用广泛。C 语言的设计目标是提供一种能以简易的方式编译、处理低级存储器、产生少量的机器码以及不需要任何运行环境支持便能运行的编程语言。