Using return 0;
in a function that returns void
is a constraint violation:
6.8.6.4 The return statement
Constraints
1 A return
statement with an expression shall not appear in a function whose return type
is void
. A return
statement without an expression shall only appear in a function
whose return type is void
.
C 2011 online draft
The compiler must issue a diagnostic for a constraint violation (the warning you saw during compilation) - however, the compiler may continue to translate the program and generate an executable (the difference between a warning and an error, and whether it halts translation, is up to the implementation).
Secondly, unless your compiler's documentation explicitly lists void
as a valid return type for main
, using void main
results in undefined behavior - at that point, the compiler is not required to handle the situation in any particular way.
Usually, trying to figure out how you got a specific result from undefined behavior is a waste of time, since the behavior may not be repeatable. In this case, you told the compiler main
wasn't going to return a value and it generated the machine code accordingly. It's likely that the register used for that return value was overwritten as soon as main
returned.