我想知道如果我忽略了K&R函数定义中的参数声明,会发生什么,所以我尝试了以下几点:
#include <stdio.h>
void f(a)
// int a; <--- omitting `declaration-list` causes warning in gcc, but nothing in clang
{
printf("%d", 9);
}
int main()
{
f(9);
return 0;
}当我用gcc 11编译它时,诊断是以警告(即warning: type of 'a' defaults to 'int')的形式发出的。但是,当我用clang 14编译它时,不会发出诊断消息。
这让我感到困惑,因为在标准中,省略declaration-list (其中包含参数声明)是一种约束冲突(按照C11 6.9.1(6)),这需要一个符合的实现来发出诊断(如C11 5.1.1.3)。那么,为什么不发出警告?我是不是误解了什么?
发布于 2022-12-03 04:09:56
如果使用-pedantic标志编译,clang将生成警告。
<source>:3:8: warning: parameter 'a' was not declared, defaulting to type 'int' [-Wpedantic]
void f(a)
^
1 warning generated.
ASM generation compiler returned: 0
<source>:3:8: warning: parameter 'a' was not declared, defaulting to type 'int' [-Wpedantic]
void f(a)
^
1 warning generated.
Execution build compiler returned: 0
Program returned: 0
9https://stackoverflow.com/questions/74663869
复制相似问题