c - 向参数为 0 的函数发送参数时的行为不一致

标签 c gcc visual-studio-2013 clang

以下代码在 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/

相关文章:

根据出生日期计算年龄

c - fstream vector C

c - MinGW gcc 设置 fp 舍入模式

将静态链接的 elf 二进制文件转换为动态链接的

c - 执行dir命令在C中搜索chrome.exe :\

asp.net-mvc - 临时目录中自动生成的 "App_Web********.cs"文件导致构建错误

sql-server - SQL Server/SQL Azure 架构比较

asp.net - 如何强制 IIS 8.5 Web 部署仅使用 SSL (HTTPS)

c++ - GCC:在 "control reaches end of non-void function"中断编译

c++ - Openfstexample.cpp :1:24: fatal error: fst/fSTLib. h: 没有这样的文件或目录编译终止