Anyone please tell me where the main() function of the 'C' language returns its value?
-
2Numerous duplicates, e.g. http://stackoverflow.com/questions/2456335/return-value-of-main-in-windows – Paul R Jan 27 '11 at 16:25
-
It returns a value to whoever started it up. If you started it from a shell, it returns the value to the shell. If you started it from an IDE (i.e. Visual Studio), then the return code is sent to Visual Studio. If you double clicked an executable on the desktop... well, it gets returned to whichever Windows process spawned him. – Gillespie Feb 09 '16 at 20:16
5 Answers
C's main
function returns an int... that int goes to the program which executed it (the parent process, if you will) as an exit status code.
Specifically, on most operating systems, a 0 exit code signifies a normal run (no real errors), and non-zero means there was a problem and the program had to exit abnormally.

- 45,269
- 12
- 110
- 134
-
3
-
2More generally, it goes to the process that invoked the executable. – Dave Costa Jan 27 '11 at 16:44
-
-
1@Jason How does it get from one process to the shell without going through the OS? – David Heffernan Jan 27 '11 at 18:02
-
1@Platinum It doesn't go to the parent process. The OS collects the return code when the process dies. There a parent process might have closed but the return value can still be read by other interested processes. – David Heffernan Jan 27 '11 at 18:03
-
@David Heffernan: Edit it yourself then, I'm tired of arguing semantics. I have to imagine it'd be largely platform-dependent exactly HOW the exit code gets to the parent process anyway. – Platinum Azure Jan 27 '11 at 18:12
-
Tangentially relevant, stdlib.h defines EXIT_SUCCESS and EXIT_FAILURE for platform-independently indicating the successful or botched run of the program. – YSN Jan 27 '11 at 18:41
The return value if the main() function is used as the exit status code of the program.
In a shell you can get the exit status of a program using $?, example:
./prog
exit_status=$?

- 98,321
- 23
- 206
- 194
From the C99 Standard:
5.1.2.2.3 Program termination
1 If the return type of the
main
function is a type compatible withint
, a return from the initial call to themain
function is equivalent to calling theexit
function with the value returned by themain
function as its argument;
and then
7.20.4.3 The
exit
function5 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 isEXIT_FAILURE
, an implementation-defined form of the status unsuccessful termination is returned. Otherwise the status returned is implementation-defined.
In short, the return value of main
is returned to the host environment in an implementation-defined form.
A general statement is: Function returns a value to the host environment.
So main() will return value to any program or shell which is hosting that piece of code or to the OS.
return value 0 is considered as successful execution

- 1,575
- 1
- 17
- 37
The main
function is at libery to return its value at any point at which it pleases. You simply write:
return my_return_value;
and it's game over.

- 601,492
- 42
- 1,072
- 1,490
-
@Jason Like I said, *at any point at which it pleases*. Once its done, it returns its return value. – David Heffernan Jan 27 '11 at 16:33
-
@Jason Are interpreting that the question is regarding how a process can read the return value of another process? – David Heffernan Jan 27 '11 at 16:34