以下代码在 clang (3.6.0) 和 gcc(4.9.2) ( coliru link ) 上都会产生编译错误
#include <stdio.h>
void foo(void){
printf("lalala\n");
}
int main(void)
{
foo(1, 2, 3, 4, 5, 6, 67);
return 0;
}
而 VS2013(使用/TC/W4 编译时)只产生警告
warning C4087: 'foo' : declared with 'void' parameter list ... 9 1
这是 VC 中的错误还是 clang 和 gcc 太苛刻了?
最佳答案
来自6.5.2.2 函数调用:
2 - If the expression that denotes the called function has a type that includes a prototype, the number of arguments shall agree with the number of parameters. [...]
VC 被故意放宽,以允许编译(为了兼容)不正确的代码。标准允许这样做,因为在遇到某些类型的不正确代码时,只要还发出诊断信息,就允许发出程序。
关于c - 向参数为 0 的函数发送参数时的行为不一致,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29986499/