16,466
社区成员
发帖
与我相关
我的任务
分享
//test.c, test.cpp
#include <stdio.h>
void test()
{
int n = 123;
int a = 0;
int b = 0;
int xNo = 0;
for (int k = 0; k < 3; k++) {
int d = 888;
Lab:
d++;
a++;
if (xNo == 3) {
return;
}
printf("a=%d\n",a);
printf("k=%d\n",k);
printf("d=%d\n",d);
}
for (int j = 0; j < n; j++) {
b++;
if (j == 8) {
xNo++;
printf("=========\n");
printf("xNo=%d\n", xNo);
goto Lab;
}
}
}
int main()
{
test();
}
$ ./a.exe
a=1
k=0
d=889
a=2
k=1
d=889
a=3
k=2
d=889
=========
xNo=1
a=4
k=3
d=890
=========
xNo=2
a=5
k=4
d=891
=========
xNo=3
g++ t.cpp
t.cpp: In function 'void test()':
t.cpp:13:2: error: jump to label 'Lab' [-fpermissive]
Lab:
^~~
t.cpp:31:9: note: from here
goto Lab;
^~~
t.cpp:11:7: note: skips initialization of 'int d'
int d = 888;
^
t.cpp:10:11: note: skips initialization of 'int k'
for (int k = 0; k < 3; k++) {
^
1>------ 已启动生成: 项目: test, 配置: Debug Win32 ------
1>t.cpp
1>c:\users\user\source\repos\test\test\t.cpp(31): error C2362: “goto k”跳过了“Lab”的初始化操作
1>c:\users\user\source\repos\test\test\t.cpp(10): note: 参见“k”的声明
1>c:\users\user\source\repos\test\test\t.cpp(13): note: 参见“Lab”的声明
1>c:\users\user\source\repos\test\test\t.cpp(31): error C2362: “goto d”跳过了“Lab”的初始化操作
1>c:\users\user\source\repos\test\test\t.cpp(11): note: 参见“d”的声明
1>c:\users\user\source\repos\test\test\t.cpp(13): note: 参见“Lab”的声明
1>已完成生成项目“test.vcxproj”的操作 - 失败。
========== 生成: 成功 0 个,失败 1 个,最新 0 个,跳过 0 个 ==========