2

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.

herohuyongtao
  • 49,413
  • 29
  • 133
  • 174
user3328692
  • 99
  • 1
  • 2
  • 8
  • See: http://stackoverflow.com/questions/1101957/are-there-any-standard-exit-status-codes-in-linux not quite a duplicate... but pretty close. – Gray Mar 18 '14 at 16:51
  • Your compiler does not execute function `exit`. It simply compiles the call to `exit(0)` into a few op-codes for pushing `0` into the stack, and then jumping (changing the instruction-pointer) to the address of function `exit`, 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 symbol `exit` with the actual address. So to summarize this - the compiler replaces `exit(0)` with binary code for jumping to a function, and the linker updates that binary code with the address of the function. – barak manos Mar 18 '14 at 17:06

5 Answers5

4
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.

herohuyongtao
  • 49,413
  • 29
  • 133
  • 174
  • 1
    Usually 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` and `EXIT_FAILURE` actually have any meaning in the program itself? I.e., does `exit` behave differently depending on which value you pass it? – brianmearns Mar 18 '14 at 17:00
  • @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. – herohuyongtao Mar 18 '14 at 17:05
  • 1
    It doesn't matter than `exit` is a built-in function. gcc is following C90 rules, which means it assumes a declaration for `exit` 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 Mar 18 '14 at 17:07
  • what is exit failure? if you wanted it to exit and it did, why is it a failure? – user3328692 Mar 18 '14 at 17:11
  • @user3328692 It means something happens and you want to notice the host environment after its termination. – herohuyongtao Mar 18 '14 at 17:18
  • @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); – user3328692 Mar 18 '14 at 17:26
  • @user3328692 If the host also thinks so. :P – herohuyongtao Mar 18 '14 at 17:28
  • @herohuyongtao i dont get you? what host? i am new to coding – user3328692 Mar 18 '14 at 17:31
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.

devnull
  • 118,548
  • 33
  • 236
  • 227
  • The 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. – Keith Thompson Mar 18 '14 at 17:08
2

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.)

Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
0

exit(0) with a 0 is sent by a process if it has ended correctly. The other numbers are used when the exit has been produced with some kind of error.

At the same time that you allow the parent process to catch exit signals from its child processes with waitpid:

  • in this way if you make processes end concurrently: while(waitpid(1,&exit_code,0) > 0), where exit_code is an integer that gets the exit code of the finished process

  • or in this way: waitpid(-1,NULL,0) if you make them finish sequentially

you can get the exit status of child processes if you put this code after waitpid or into waitpid's loop:

pid = waitpid(-1, &exit_code, 0);  //getting the finished process' PID

if (WIFEXITED(exit_code)) { 

int statcode = WEXITSTATUS(exit_code); 
printf(“Process with PID %d has finished with status %d, pid, statcode); 

}

try this if you want to make sure that exit() works

0

Just use the <stdlib.h> header file and things will work!