In my programming book, it shows me exit used without any parameters(exit();
).
Unfortunately it does not work.
Some people have said use exit(0);
while some say exit(1); exit(2);
and exit(3)
;
What is the difference between them and is there even an exit(4);
?
The funny thing is that my compiler does not need stdlib.h
to execute exit(0);
and the rest.
Prior to the 1999 version of the ISO C standard, it was legal to call a function with no visible declaration. The compiler would assume that the function exists, creating an implicit declaration. (It would also assume that it returns a result of type int
, which exit()
does not.) If this implicit declaration doesn't match the actual definition of the function, the behavior is undefined.
As of the 1999 standard, the "implicit int
" rule was dropped, and a call without a visible declaration (as provided, in this case, by #include <stdlib.h>
) became invalid. Even though it's invalid, a compiler may still issue a non-fatal warning and handle it under the older rules; gcc does this by default.
Under any version of the language, exit
requires a single argument of type int
. Passing 0
or EXIT_SUCCESS
(a macro defined in <stdlib.h>
causes the program to terminate and pass a status to the environment indicating success. Passing EXIT_FAILURE
causes the program to terminate with a status indicating failure.
The meanings of other argument values are not specified by the C language. You'll commonly see exit(1)
to denote failure, but that's not entirely portable.
(exit
may be some kind of built-in function in gcc, but that doesn't affect the rules of the language; it's still invalid to call exit
with no visible declaration, or to call it without an int
argument. If it's built-in, that might affect the level of detail in the diagnostic message.)