当找不到匹配函数时,ac编译器会做什么

sas*_*har 4 c function c99

请考虑以下代码:

#include<stdio.h>
int f()
{
        printf(" hey ");
        return 5;
}
int main()
{
        printf("hello there %d",f(4,5));
        f(4,5);
        return 0;
}
Run Code Online (Sandbox Code Playgroud)

我期望函数'int f()'的参数太多,但即使在严格的C99编译中也能提供输出.为什么会这样?但似乎C++编译器会出错.

Kon*_*lph 8

在某些方面,C比C++严格得多.

f()C++中的函数签名表示不带参数的函数.相反,在C中,它表示未指定参数的函数.可以使用任意数量的参数调用它.

要防止这种情况,请使用以下原型:

int f(void)
Run Code Online (Sandbox Code Playgroud)