Does exit() do anything special that 'return' doesn't?
With some compilers for uncommon platforms, exit()
might translate its argument into your program's exit value while a return from main()
might just pass the value directly to the host environment without any translation.
The standard requires identical behavior in these cases (specifically, it says returning something that's int
-compatible from main()
should be equivalent to calling exit()
with that value). The problem is that different OSes have different conventions for interpreting the exit values. On many (MANY!) systems, 0 means success and anything else is a failure. But on, say, VMS, odd values mean success and even ones mean failure. If you returned 0 from main()
, a VMS user would see a nasty message about an access violation. There wasn't actually an access violation--that was simply the standard message associated with failure code 0.
Then ANSI came along and blessed EXIT_SUCCESS
and EXIT_FAILURE
as arguments you could pass to exit()
. The standard also says that exit(0)
should behave identically to exit(EXIT_SUCCESS)
, so most implementations define EXIT_SUCCESS
to 0
.
The standard, therefore, puts you in a bind on VMS, as it leaves no standard way to return a failure code that happens to have the value 0.
The early-1990s era VAX/VMS C compiler therefore did not interpret the return value from main()
, it simply returned whatever value to the host environment. But if you used exit()
it would do what the standard required: translate EXIT_SUCCESS
(or 0
) into a success code and EXIT_FAILURE
into a generic failure code. To use EXIT_SUCCESS
, you had to pass it to exit()
, you could not return it from main()
. I don't know whether more modern versions of that compiler preserved that behavior.
A portable C program used to look like this:
#include <stdio.h>
#include <stdlib.h>
int main() {
printf("Hello, World!\n");
exit(EXIT_SUCCESS); /* to get good return value to OS */
/*NOTREACHED*/ /* to silence lint warning */
return 0; /* to silence compiler warning */
}
Aside: If I recall correctly, the VMS convention for exit values is more nuanced than odd/even. It actually uses something like the low three bits to encode a severity level. Generally speaking, however, the odd severity levels indicated success or miscellaneous information and the even ones indicated errors.