4

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

Pushpendra
  • 4,344
  • 5
  • 36
  • 64
  • 2
    Numerous 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 Answers5

9

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.

Platinum Azure
  • 45,269
  • 12
  • 110
  • 134
  • 3
    It might not go to the OS, it might go to the shell. – jason Jan 27 '11 at 16:31
  • 2
    More generally, it goes to the process that invoked the executable. – Dave Costa Jan 27 '11 at 16:44
  • Mmm, that's true. Let's fix that. – Platinum Azure Jan 27 '11 at 17:43
  • 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
4

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=$?
Arnaud Le Blanc
  • 98,321
  • 23
  • 206
  • 194
2

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.

Community
  • 1
  • 1
R Sahu
  • 204,454
  • 14
  • 159
  • 270
0

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

Daemon
  • 1,575
  • 1
  • 17
  • 37
0

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.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490