Pushpendra
Pushpendra

Reputation: 4392

main() functions return value?

Anyone please tell me where the main() function of the 'C' language returns its value?

Upvotes: 5

Views: 17342

Answers (5)

R Sahu
R Sahu

Reputation: 206747

From the C99 Standard:

5.1.2.2.3 Program termination

1 If the return type of the main function is a type compatible with int, a return from the initial call to the main function is equivalent to calling the exit function with the value returned by the main function as its argument;

and then

7.20.4.3 The exit function

5 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.

In short, the return value of main is returned to the host environment in an implementation-defined form.

Upvotes: 2

Daemon
Daemon

Reputation: 1675

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

Upvotes: 0

Platinum Azure
Platinum Azure

Reputation: 46233

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.

Upvotes: 9

David Heffernan
David Heffernan

Reputation: 613602

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.

Upvotes: 0

Arnaud Le Blanc
Arnaud Le Blanc

Reputation: 99929

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=$?

Upvotes: 4

Related Questions