1

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?

Community
  • 1
  • 1
Yishu Fang
  • 9,448
  • 21
  • 65
  • 102
  • 1
    http://stackoverflow.com/questions/204476/what-should-main-return-in-c-c See this – sinni800 Nov 03 '12 at 04:31
  • Why this problem is closed due to the "Duplicate" issue? That problem mainly deals with what the meaning of the return value, but my problem mainly concentrate on how to get/display and how to use that value. @sinni800 – Yishu Fang Nov 10 '12 at 13:28
  • I don't know, but this question I linked also answers your question just by the way – sinni800 Nov 10 '12 at 23:42

8 Answers8

4

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.

Alok Save
  • 202,538
  • 53
  • 430
  • 533
3

On unix-like systems, the result code can be displayed like this:

$ ./myprogram
$ echo $?
8
$
Vaughn Cato
  • 63,448
  • 5
  • 82
  • 132
2

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;
}
Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
1

return 0 means normal exit. return x with x != 0 means termination with "error".

dreamcrash
  • 47,137
  • 25
  • 94
  • 117
0

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.

a-z
  • 1,634
  • 4
  • 16
  • 35
0

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.

Tux
  • 1,896
  • 3
  • 19
  • 27
0

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.

Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331
0

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.

leonidus
  • 246
  • 1
  • 7