[Zlib-devel] exit() vs. return in main()
Cosmin Truta
cosmin at cs.toronto.edu
Thu Mar 13 15:21:03 EST 2003
Thanks, Nelson, for the detailed analysis.
> The exit value 0 was historic on Unix, but other operating systems
> have had different conventions, so EXIT_SUCCESS and EXIT_FAILURE were
> introduced into the C and C++ Standards for O/S portability. They
> also give an obvious name to an otherwise-magic constant.
There are reasons to worry, though, about platforms that don't have
EXIT_SUCCESS etc. defined in stdlib.h.
The programs that I am talking about are written in K&R style., so I
assume they are not necessarily intended for an ANSI-compliant compiler.
The compilation may break, and this is a much bigger problem.
Also, C++ doesn't even accept the K&R syntax, so the issue does not
apply there.
I admit that "int main() { ... return 0; }" is better, compliance-wise,
than "void main() { ... exit(0); }". Style-wise, however, the former may
force some inconsistency: what if you want to exit() from a different
function, not necessarily from main()?
Java, for example, does it the right way. Not only that main() is void,
but also you don't have to write any noisy "exit(something_long)" if
everything goes fine. You just exist normally, as from any other void
function that finished its task properly.
In conclusion, I suggest replacing every "... exit(0); return 0;"
with a simple "return 0;" in all main() functions.
Cosmin
More information about the Zlib-devel
mailing list