0

I am going through a text on C . And I came across the following line about exit function but I couldn't understand it completely . Here's the line :

"The argument of exit is available to whatever process called this one , so the success or failure of this program can be tested by another program that uses this one as a sub process "

How is the argument of exit available to the processes that use a program which calls it ? Is it so that exit returns whatsoever value that was provided to it as an argument ?

P.S : On being aware of possible similarities with the following question :

How can I get what my main function has returned?

I would like to say that I am not that deep into C to ascertain that the answers required meet up the demands of the question I posted. Is the value returned by exit same as the one returned by main ? And my question was about how the argument provided to exit in a program is available to any other program which calls the former . Still then , please guide me if the answers in both the places conceptually coincide.

Community
  • 1
  • 1
dead poet
  • 23
  • 5
  • 2
    Works in a similar way that `return` does. `return` returns the value to whatever called the function, `exit` returns the value to the process that called the program, whether that be the shell or something else. The calling program has to capture the value returned in order to use it. – AntonH Mar 12 '17 at 04:50
  • 1
    In a unix-like shell you can see the exit code of the previous program by writing `echo $?` – M.M Mar 12 '17 at 05:01
  • 1
    in a Windows shell it's possible to see the exit code by `echo %errorlevel%` or test it with `if errorlevel` – phuclv Mar 12 '17 at 05:31
  • Possible duplicate of [How can I get what my main function has returned?](http://stackoverflow.com/questions/8626109/how-can-i-get-what-my-main-function-has-returned) – phuclv Mar 12 '17 at 05:31
  • @AntonH : Difference between returning to a program and returning to a process ? – dead poet Mar 12 '17 at 05:38
  • You may find the information in [ExitCodes bigger than 255 — possible?](http://stackoverflow.com/questions/179565/exitcodes-bigger-than-255-possible/) useful. – Jonathan Leffler Mar 12 '17 at 08:16

2 Answers2

2

Yes, basically your understanding it correct.

Calling exit() terminates a process and upon termination, the caller of the process (generally, another process belonging to the host environment) receives the value (in an implementation defined manner, see below) supplied as argument to exit() in the called process as a return value from the call.

Quoting C11, chapter §7.22.4.4, The exit function

_Noreturn void exit(int status);

The exit function causes normal program termination to occur. [...]

and

Finally, control is returned to the host environment. If the value of status is zero or EXIT_SUCCESS, an implementation-defined form of the status successful termination is returned. If the value of status is EXIT_FAILURE, an implementation-defined form of the status unsuccessful termination is returned. Otherwise the status returned is implementation-defined.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • of course it must be defined by each implementation. Every OS is vastly different. The way they execute a process is different, so how can C standard enforce how should they return the value to the calling process? – phuclv Mar 12 '17 at 07:08
0

What you are describing is called an "Exit Code". Exit codes are used in the exit() function to end the program. Exit code is typically 0 for success.

For example:

int* i;
if((i = malloc(...)) == NULL) {
  exit(1);
}

This exits the program with your predefined exit code 1 if you have a memory allocation error.

Bennett Yeo
  • 819
  • 2
  • 14
  • 28
  • `return` at the end of `main` also returns the exit code. Exit is only needed when you need to exit from inside a function – phuclv Mar 12 '17 at 07:10