This question already has an answer here:
I was wondering when should I use
exit() function over
return statement. I can end the program with either of the following statements:
Which one should I use and when? Is there any advantage of using
Best How To :
These two are very different in nature.
exit() is used when you want to terminate program immediately. If a call to
exit() is encountered from any part of the application, the application finishes execution.
return is used to return the program execution control to the caller function. In case of
return finishes the execution.
To clarify about the case when used in
main(), quoting directly from the
C11 standard, chapter §188.8.131.52.3, Program termination,
return type of the
main() function is a type compatible with
return from the initial call to the
main() function is equivalent to calling the
exit() function with the value returned by the
main() function as its argument;11) reaching the
} that terminates the
main() function returns a value of
0. If the return type is not compatible with
int, the termination status returned to the host environment is unspecified.
So, basically, either
will behave as same in the context of