f() does not return even though it’s signature say it should.
Why is the reason for allowing this to compiling?
Is there a reason the C standard does not require the compiler to make it fail?
I know that it is Undefined behavior and all, but why is it allowed in the first place?
Is there a historical reason?
double f(){}
int main()
{
f();
return 0;
}
By invoking undefined behavior, the C standard allowed the compilers to be less complicated. There is indeed some cases, such as
ifstatements, in which it is hard to say whether the function returns a value or not:f(5), it is easy for the compiler to say that thefunction is correct.
f(-5), it is also easy to detectan undefined return value.
But if the argument comes from user input for example, how should the compiler be able to know whether the function returns a value? Since this could both a valid or a wrong program, C standard allows the compilers to do what they want. C is designed to be as smart and simple as possible.