문제

Does the following program invoke Undefined Behaviour in C?

int main()
{
    printf("Printf asking: Where is my declaration ?");
}

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?

도움이 되었습니까?

해결책

Yes it does. Not having a declaration in scope is 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).

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top