gpt4 book ai didi

c - 为什么在函数末尾放置标签时必须包含分号?

转载 作者:行者123 更新时间:2023-12-04 02:27:35 25 4
gpt4 key购买 nike

带有 goto 错误处理程序的简单函数:

void checkFile(char *name)
{
FILE *fp = fopen(name, "r");
if(NULL == fp)
{
fprintf(stderr, "Could not open %s\n", name);
goto end;
}

// use fp...

fclose(fp);
end:
;
}

注意,如果我在 end: 之后删除无用的分号,函数将无法编译。

在 GCC 上:

error: expected primary-expression before '}' token
16 | }

在 MSVC 上:

error C2143: syntax error: missing ';' before '}'

所以,我知道 C 标准确实说 goto 关键字的目标需要在 § 6.8.6.1 p 2 中声明:

A goto statement causes an unconditional jump to the statement prefixed by the named label in the enclosing function

但是,仅仅因为标签存在,错误就存在;如果我删除 goto 关键字,标签本身仍被视为错误并且不会编译。我阅读了标准中关于“标记语句”的部分(§ 6.8.1),但仍然没有找到任何可以解释这种奇怪约束的内容。

最佳答案

在 C 中,标签可以放在语句之前。因此,如果没有语句,您可以放置​​一个空语句。

来自 C 标准(6.8.1 标签语句)

labeled-statement:
identifier : statement
case constant-expression : statement
default : statement

And(6.8.3 表达式和空语句)

expression-statement:
expressionopt ;

3 A null statement (consisting of just a semicolon) performs nooperations.

在 C++ 声明中与 C 相对的也是语句。所以在 C++ 中,您可以在声明之前放置一个标签。

这里是演示程序。

C 程序。

#include <stdio.h>

int main(void)
{
goto End;

End:;
const char *bye = "Bye";

puts( bye );

return 0;
}

程序输出为

Bye

C++程序

#include <iostream>

int main()
{
goto End;

End:
const char *bye = "Bye";

std::cout << bye << '\n';

return 0;
}

程序输出为

Bye

注意C程序中标号后有空语句

End:;

没有它编译器会报错。

关于c - 为什么在函数末尾放置标签时必须包含分号?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66447253/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com