gpt4 book ai didi

c - 是自动的;有效的 C 翻译单元?

转载 作者:太空狗 更新时间:2023-10-29 17:24:17 24 4
gpt4 key购买 nike

创建文件test.c,包含以下代码:

auto;

用 clang 6.0 编译它:clang -c test.c。它将成功生成一个目标文件 test.o,尽管它没有实际内容(目标文件头除外)。它会打印一条警告,但仍然接受它作为有效代码:

test.c:1:1: warning: declaration does not declare anything [-Wmissing-declarations]
auto;
^~~~
1 warning generated.

相比之下,gcc 4.9 拒绝编译 test.c,产生错误:

test.c:1:1: error: 'auto' in file-scope empty declaration
auto;
^

为什么 clang 生成警告但接受此翻译单元有效,而 gcc 生成错误并拒绝编译它?谁的行为更符合C标准?允许什么都不声明的自动声明有什么意义?

最佳答案

这不是有效代码。但是在发出诊断消息后,实现可以自由地赋予它任何它想要的含义。

5.1.1.3 Diagnostics

1 A conforming implementation shall produce at least one diagnostic message (identified in an implementation-defined manner) if a preprocessing translation unit or translation unit contains a violation of any syntax rule or constraint, even if the behavior is also explicitly specified as undefined or implementation-defined. Diagnostic messages need not be produced in other circumstances.9)
[...]

6.7 Declarations

Constraints
2 A declaration other than a static_assert declaration shall declare at least a declarator (other than the parameters of a function or the members of a structure or union), a tag, or the members of an enumeration.
[...]

引自 C99+修正案(C11、n1570)

关于c - 是自动的;有效的 C 翻译单元?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26824137/

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