C99 6.9.1/12 "Function definitions" says:
If the } that terminates a function is reached, and the value of the function call is used by the caller, the behavior is undefined.
C90 6.6.6.4 "The return statement"standard says something with simialr effect:
If a return statement without an expression is executed, and the value of the function call is used by the caller, the behavior is undefined. Reaching the } that terminates a function is equivalent to a return statement without an expression.
So returning from a function without returning something from the function is permitted - but the caller of the function isn't allowed to use the 'result' of the function call. That's undefined behavior, and like other answers have mentioned you can get 'expected' results with undefined behavior, but that's only by chance.
I believe the rationale for this is that pre-standard C defaulted to a return type of int for functions if the function wasn't explicitly declared (or if the declaration omitted the return type), and that continued to be supported when C was standardized. Many of those functions were designed to be called only for side-effects they produced, and no return value was expected or provided.
A couple side notes:
- gcc will warn about this if the
-Wall option is used. MSVC warns about it by default.
- you should be getting a compiler error about the
if (flag < 0) break; line since the break isn't in a loop or switch.
main() should return int not void (-Wall will warn about that as well). Of course, you should also return some value explicitly...