下面的程序是否调用了 C 中的 Undefined Behaviour
?
int main()
{
printf("Printf asking: Where is my declaration ?");
}
在上面的程序中有一个printf()
的隐式声明,那么上面的代码是完全符合标准还是只是有一些特定于实现的行为?
最佳答案
是的。在范围内没有声明是 UB。
J.2 Undefined behavior
— For call to a function without a function prototype in scope where the function is defined with a function prototype, either the prototype ends with an ellipsis or the types of the arguments after promotion are not compatible with the types of the parameters (6.5.2.2).
另外,请注意,在 C99 中,从 main 中退出是可以的(即在语义上等同于 return 0;
)。对于 C99 之前的兼容编译器,您需要一个 return 语句,其中 main 函数的返回类型是与 int
兼容的类型。
关于c - C 中的隐式声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3528164/