Why don't standard let functions return compilation?

f () is not returned, even if the signature should say. Why is there a reason to compile it? Is there a reason why the C standard does not require a compiler error?

I know that this is Undefined behavior and that’s all, but why is it allowed in the first place? Is there a historical reason?

double f(){}

int main()
{
    f();
    return 0;
}
+5
source share
4 answers

Is there a reason why the C standard does not require the compiler to make it not work?

By invoking undefined behavior, the C standard allowed compilers to be less complex. Indeed, there are some cases, for example, expressions ifin which it is difficult to say whether a function returns a value or not:

int f(int n)
{
  if (n > 0) return 1;
}
  • f(5), , .
  • f(-5), undefined.

, , , ? , , C , . C .

+4

, , . , , , .

, - undefined :

}, , , , undefined.

+1

, , . , (, abort()).

undefined C99, . 6.9.1/12:

}, , , , .

, , .

++ [diff.stat]. , C , , C int return void. , , - "". , AFAIK C double return int, , , UB , double, , .

, , tricker:

double f() {
    if (g()) exit();
}

, , g . , , , C, , , , . g TU, , , , .

, , , , , . - , UB, , , . , C, , .

+1

, .

0

All Articles