0
int main()
{
    return 1;
}

1 is getting returned to whom in this case? As in if function x() is calling function y() and y() is returning something then x() gets it.So in above case who is catching 1 returned from main.

A-B
  • 31
  • 3
  • The one who starts the process – Sami Kuhmonen Feb 09 '16 at 19:47
  • 1
    On some operating systems and for some compilers, the return value from main will be interpreted as the process exit code. That's nothing to do with the C standard afaik, just an implementation choice sometimes made. – Eric J. Feb 09 '16 at 19:48
  • Its returning to operating system to let it know that `main` executed and exited successfully. – haccks Feb 09 '16 at 19:48
  • Actually, it tells the operating system that the return value is 1. Most people will interpret a return value of zero as successful, and any other value as an error. – FredK Feb 09 '16 at 19:50
  • C started off as the systems programming language for Unix. On Unix-like operating systems, the return value translates to an exit value, which is passed to the OS kernel. The kernel then makes it available to the parent process that had launched the process that just exited. The parent process then should collect the exit value from the kernel in order to 1) learn whether the run was a success (or if not, then how it failed) and 2) to let the kernel know that the process id slot for the exitted process is now available to be reused. Other systems may do their own thing with the return value. – Petr Skocik Feb 09 '16 at 19:57

1 Answers1

0

main returns its value to some magic run-time start-up library code (that you didn't write and you generally can't see). But, depending on your OS, the value may make it out to your user environment. For example, on a Unix or Linux system, if I have five.c containing

int main()
{
    return 5;
}

and if I do

cc five.c
a.out
echo $?

I'll see "5" as the exit status of a.out.

Steve Summit
  • 45,437
  • 7
  • 70
  • 103