Because it's not an error -- it's undefined behavior. See section 6.9.1, paragraph 12 of the C99 standard:
If the } that terminates a function is reached, and the value of the function call is used by the caller, the behavior is undefined.
Thus, the compiler is free to do whatever it wants when seeing your code failing to return -- it can emit an error, a warning, or nothing at all. In the case of GCC, it by default compiles successfully with no warnings or errors. With the -Wall
option, it emits a warning.
main()
is special in C: it's the only function that's allowed not to return a value. The C standard says that if control reaches the end of main()
without a return
statement, it implicitly returns 0. This is only true for main()
, all other non-void functions must return a value.
Section 5.1.2.2.3:
If the return type of the main function is a type compatible with int, a return from the
initial call to the main function is equivalent to calling the exit function with the value returned by the main function as its argument;10) reaching the } that terminates the
main function returns a value of 0. If the return type is not compatible with int, the
termination status returned to the host environment is unspecified.