Question

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?

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top