Anyone please tell me where the main() function of the \'C\' language returns its value?
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.
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.
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=$?
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
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.