K
Keith Thompson
Anton Petrusevich said:2 variants: either it works or it does not. And only one of them will be
chosen on some particular system/compilator. I would not call it "undefined
behaviour", it's quite defined for a platform. And I just want to know the
name of the platform where it does not work. Could you enlighten me?
void main() is actually an unusual case, and perhaps not the best one
for illustrating the concept of undefined behavior.
The standard explicitly allows an implementation to define the
behavior of alternative definitions of main(), including void
main(void), but it does not require any implementation to do so. For
an implementation whose documentation *doesn't* mention void main(void),
the behavior of
void main(void){}
is undefined (see also "nasal demons"). For an implementation that
*does* explicitly document it, the behavior is implementation-defined.
In a sense, the extra permission given by the standard is redundant,
since an implementation is free to document anything it likes,
including defining the behavior of constructs that, as far as the
standard is concerned, invoke undefined behavior.
In a way, it's similar to what happens on integer overflow. The
following code fragment:
int i = 32767;
i ++;
invokes undefined behavior under an implementation with
INT_MAX == 32767, but is well defined where INT_MAX > 32767.
Bottom line: always use one of the standard definitions of main()
unless you have a very good reason not to. Saving a few keystrokes is
not a very good reason. Using a freestanding implementation (most
likely an embedded system) may be a very good reason.
In response to comments made elsewhere in this thread, none of this
has anything whatsoever to do with "religion". We are simply giving
you the best advice we can. If this comes across as "You must do
things this way because you'll go to Hell if you don't!", please feel
free to ask for clarification. There's almost always a valid reason
for the advice we give, but we don't always necessarily make that
sufficiently clear.