exit(); is not working
Solution 1
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.)
Solution 2
void exit( int exit_code );
Here, exit_code
is the exit status of the program. After calling this, control is returned to the host environment. If exit_code
is EXIT_SUCCESS
, an implementation-defined status, indicating successful termination is returned. If exit_code
is EXIT_FAILURE
, an implementation-defined status, indicating unsuccessful termination is returned. In other cases implementation-defined status value is returned.
Check out here for more info.
P.S.: The reason that your compiler does not need stdlib.h
to execute exit(0);
maybe either it has been include by other headers that included in your code or, as @devnull mentioned, when building using gcc
where exit()
is one of the built-in functions.
Solution 3
The funny thing is that my compiler does not need stdlib.h to execute exit(0);and the rest.
You seem to be using gcc
. exit
is one of the built-in functions provided by gcc, due to which you do not need the specified header.
The parameter passed to exit()
is used to indicate termination status.

user3328692
Updated on June 04, 2022Comments
-
user3328692 5 months
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 sayexit(1); exit(2);
andexit(3)
; What is the difference between them and is there even anexit(4);
?The funny thing is that my compiler does not need
stdlib.h
to executeexit(0);
and the rest.-
Gray over 8 yearsSee: stackoverflow.com/questions/1101957/… not quite a duplicate... but pretty close.
-
barak manos over 8 yearsYour compiler does not execute function
exit
. It simply compiles the call toexit(0)
into a few op-codes for pushing0
into the stack, and then jumping (changing the instruction-pointer) to the address of functionexit
, within the code-section of your executable image. And since the address of that function is not known during compilation, it is the linker which replaces the symbolexit
with the actual address. So to summarize this - the compiler replacesexit(0)
with binary code for jumping to a function, and the linker updates that binary code with the address of the function.
-
-
brianmearns over 8 yearsUsually the exit code is only relevant to whoever or whatever called the program. By convention, an exit code of 0 usually indicates success and non-zero exit codes represent specific errors as defined for the program. But this is only a convention. Do you know if
EXIT_SUCCESS
andEXIT_FAILURE
actually have any meaning in the program itself? I.e., doesexit
behave differently depending on which value you pass it? -
herohuyongtao over 8 years@sh1ftst0rm After calling this, control is returned to the host environment. It will be meaningful if the host environment need to handle according this returned status.
-
Keith Thompson over 8 yearsIt doesn't matter than
exit
is a built-in function. gcc is following C90 rules, which means it assumes a declaration forexit
if none is visible. You'll see the same behavior for any undeclared function, built-in or not, though gcc may give more detailed diagnostics for built-in functions. -
Keith Thompson over 8 yearsThe fact that
exit
is built-in probably doesn't matter. I think you'll see the same behavior for any call to an undeclared function. -
user3328692 over 8 yearswhat is exit failure? if you wanted it to exit and it did, why is it a failure?
-
herohuyongtao over 8 years@user3328692 It means something happens and you want to notice the host environment after its termination.
-
user3328692 over 8 years@herohuyongtao Do you mean the parameters only mater, if you want to know the exit status. So if you do not want to know the exit status it does not matter what parameter you put in like exit(347);
-
herohuyongtao over 8 years@user3328692 If the host also thinks so. :P
-
user3328692 over 8 years@herohuyongtao i dont get you? what host? i am new to coding