Question

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.

Was it helpful?

Solution 3

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

OTHER TIPS

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.

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.

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

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top