Possible Duplicate:
What should main() return in C/C++?
I have this simple program in C:
#include <stdio.h>
int main()
{
return 8;
}
What's the meaning of the return value, and how to display it, or how to use it?
Possible Duplicate:
What should main() return in C/C++?
I have this simple program in C:
#include <stdio.h>
int main()
{
return 8;
}
What's the meaning of the return value, and how to display it, or how to use it?
What's the meaning of the return value?
It is used to return the status of the main()
function to the caller of main()
:
Reference:
C++03 Standard: 18.3 Start and termination
Finally, control is returned to the host environment. If status is zero or EXIT_SUCCESS, an implementation-defined form of the status successful termination is returned. If status is EXIT_FAILURE, an implementation-defined form of the status unsuccessful termination is returned. Otherwise the status returned is implementation-defined.
how to display it, or how to use it?
Just as you would display or use return value of any other function. main()
is just another function in C/C++ there's nothing special about main in that regards.
On unix-like systems, the result code can be displayed like this:
$ ./myprogram
$ echo $?
8
$
0
or EXIT_SUCCESS
gives some implementation defined "successful exit" value. On some systems, this wasn't necessarily passed through directly. For example, on VMS, even values signal failure and odd values indicate success, so return 0;
in the program actually returned a non-zero value (1, if I recall correctly) to the shell.
EXIT_FAILURE
gives some implementation defined "failed exit" value.
The C and C++ standards don't attempt to define anything else, but on a typical system (Windows, Linux, etc.) at least the bottom 8 bits of the value will be passed back to the environment as the return value from your program.
You can use it from another program (especially the shell) to determine whether this succeeded or failed (and if so, possibly how/why it failed). You can display it by spawning this program, collecting the returned value, and then printing it out like any other number:
#include <stdlib.h>
#include <stdio.h>
#include <process.h>
#include <signal.h>
int main(int argc, char **argv) {
int retval;
signal(SIGINT, SIG_IGN);
retval = spawnvp(P_WAIT, argv[1], argv+1);
printf("%s returned: %d\n", argv[1], retval);
return 0;
}
return 0 means normal exit. return x with x != 0 means termination with "error".
The return value is a massage at the end of runtime, 0 means there is no error and any other value is an error code.
The return value simply tells the CPU that the program either exited successfully or unsuccessfully. 0 means that the exit was successful, while 1 means that the exit was unsuccessful.
EXIT_SUCCESS is also equal to 0 and EXIT_FAILURE is also equal to 1. You can return it by using the signal()
function and the spawnvp()
function.
The C and C++ standards only define two possible meanings for the main
return value, namely process success and process failure.
Following the standard you can invoke the failure meaning by returning EXIT_FAILURE
, and you can invoke the success meaning by returning 0 or by returning EXIT_SUCCESS
, where the two named constants are macro symbols defined by <stdlib.h>
, Which, as it happens, also declares exit
and abort
.
In practice for *nix and Windows the returned value directly becomes the process exit code. The main usage of that is to execute some other program conditionally on the success or failure of this one. For example, in Windows' [cmd.exe]:
[D:\dev\test] > md blah [D:\dev\test] > (rd blah 2>nul) && echo pesky dir removed || echo oops, unable to remove dir pesky dir removed [D:\dev\test] > echo %errorlevel% 0 [D:\dev\test] > (rd blah 2>nul) && echo pesky dir removed || echo oops, unable to remove dir oops, unable to remove dir [D:\dev\test] > echo %errorlevel% 2 [D:\dev\test] > _
In both *nix and Windows you can return other values than the ones defined by the C and C++ standards.
However, in Windows a value just above 256 (I can never recall the exact value) is used as status code for polling whether a process has finished, so to avoid problems one should either stay below 256, or well above it, such as a 32 bit status code.
The returned value is sent to parent process that forked a new process and executed your program. Generally the parent is shell so it does nothing with it but if parent process is not a shell then it can be used by the parent process to know the exit status of the child and it can take appropriate actions according to it.