问题描述
执行以下程序调用未定义行为
用C?
Does the following program invoke Undefined Behaviour
in C?
int main()
{
printf("Printf asking: Where is my declaration ?");
}
在上面的程序有的printf的一个隐含的声明()
,所以上面的code完全符合标准,或者只是有一些实施的具体行为?
In the above program there is an implicit declaration of printf()
, so is the above code fully standard compliant or it just has some implementation specific behaviour?
推荐答案
是它。由于没有在一个范围内声明UB。
Yes it does. Not having a declaration in scope is UB.
J.2未定义行为
- 对于调用函数时没有
函数原型范围在哪里
函数与一个函数定义
原型,无论是原型结束
以省略号或类型
升级后的参数都没有
同类型的兼容
参数(6.5.2.2)。
— 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没关系(即语义上等价于返回0;
)。对于pre-C99标准的编译器,你需要一个return语句,其中的主函数的返回类型是一个类型兼容 INT
的
Also, note that falling off main is okay in C99 (i.e. semantically equivalent to a return 0;
). For pre-C99 compliant compilers you need a return statement where the return type of the main function is a type compatible with int
.
这篇关于用C隐宣言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!