Someone sent in this question by email:
> I observe that we always include exit(EXIT_SUCCESS) and exit(EXIT_FAILURE) in the main function in our C programs. Is it a common practice as well as a requirement for our project assignments that we should always include these two exits in our main function?
The
main()
function may return (to the operating system) by calling the
exit()
function, simply calling return, or 'dropping off' the bottom curly bracket.
In the last 2 cases, no call to
exit()
is required at all.
We an assume that the anticipated exit status of a process is success - which we can indicate with
exit(EXIT_SUCCESS)
or even
return EXIT_SUCCESS
.
A program that can never fail, presumably a very simple one or one that doesn't check enough things, will not even have a call to
exit(EXIT_FAILURE)
.
And there's a program, named
false
, that only ever exits with failure, never with success.
So there's no general rule, nor requirement, about how any calls to
exit()
a program must have.