"Ziggy" <
[email protected]> a écrit dans le message de
Hi,
Just for curiosity.
I know that main function is returning
an int, but I came cross with the following
code. (Unnessecary code is snipped) Is there
any problem with it?
#include <stdlib.h>
void main(){
exit(EXIT_SUCCESS);
}
It's an unspecified behaviour and an undefined behavior in an hosted
environment (annexes J.1 and J.2) with the current standard. Nevertheless,
the behavior can be defined by the implementation. I let you find a copy of
the standard (C99, ISO/IEC 9899:1999) to see the definitions and the
differences between these terms, especially concerning the behavior's topic.
Having a draft of C99, I would recommend to read carefully §5.1.2 (execution
environments)
At program startup:
void main() isn't of the recommend form: it shall be int main(void) or int
main(int argc, char *argv[]) , §5.1.2.2.1
....But it could be another form defined by the implementation, see annex
J.3.2
Let's come back to §5.1.2.2.3, now about program termination, in your case,
main doesn't return anything (the return type is incompatible with int), the
termination status of the program returned to the host environnement is
unspecified.
Finally, by looking at the exit() function in §7.20.4.3, if the status
(parameter) of exit is EXIT_SUCCESS or 0, an implemention-defined form of a
successful termination is returned.
All that to say, rather than digging in a compiler documentation to obtain a
*correct* program whose the behavior is well-defined thanks to
implementation specific features, the best way would probably here to follow
the standard
Regis