what does return 0 in main function do?

What instruction does it perform? it has to return a value, but then return a value where? please no assembly code as i haven't studied that yet. :)
return, in a function, is used to exit from the function, returning a value.. Main is a integer function (int main), so 0 is a integer which can be returned from it, and we use return 0 (which is not compulsory in some of the ide) at the end of the program to stop the execution...
Allright? This is a very simple explanation and I think that somebody will give you other informations, but we are in the" beginners " part of the forum, so it's good to know this, I suppose..
it has to return a value, but then return a value where?

To the operating system, and the value is called "error code".

Traditionally, an error code of 0 means "all well", anything else could mean trouble.

1
2
3
4
int main()
{
    return 77;
}


You can check the value by running this, right after you run the actual program:
--- in Windows, cmd.exe ---
echo %errorlevel%

--- in Linux, bash ---
echo $?
closed account (zb0S216C)
Return performs one specific task: to return from the function that's currently being executed. In assembly, "return" is simply boiled down to this:

 
ret

That's it. However, if a function returns some value of some type, the value specified by the return statement is pushed onto the stack, followed by the destruction of all locally declared automatic storage, including parameters. Once all automatic storage has been popped from the stack, "ret" is called and the CPU jumps back to the calling function, the caller.

Note that "main( )" is a special function, but "return" behaves the same way in all functions. In "main( )" the value returned by the process can be any value within the range of "int". To the OS, the value returned by the process is meaningless and the OS won't panic if a non-zero value is returned -- there are no standard return values except for "EXIT_SUCCESS" and "EXIT_FAILURE", but even the OS is unaware of these.

Wazzak
very helpful answers.Thanks everyone
Without using return(0) the program will close suddenly without displaying the result even though the code is correct .You can also use getch() for the same purpose but be sure to include header-file conio.h..
cheers,
cyber dude
Last edited on
well, if you don't use "return 0", it become implicit to the compiler, but you can also use other returns, like "return 1", "return 2", and even omit the return. This because a function is defined by its braces, so, when the compiler arrives at the last brace, it knows it's the end of the function. The correct thing is to write "return 0;" at the end of the function. If you write other returns, the program usually works properly, but some debuggers can give you a error message... If you don't type return 0, it doesn't matter, but it's correct to do so. Another thing.. Don't confuse return 0 and system("pause"), or other similar commands to the o.s. If you don't type system("pause") [or similar, I know it is very slow] , the program finishes immediately and you can't see the result, if you don't type return 0, the program ends well, but you can receive an error from some debuggers.
okay. it's clear as crystal now. thanks!
this works too..

1
2
3
4
   //image this was a program called SOMEPROG.EXE in the same directory..
   int main(){
      return 30;
   }


..as another program with a line like this...
 
    int ret = system("SOMEPROG");


ret should equal 30 after SOMEPROG runs and returns 30...
Last edited on
Topic archived. No new replies allowed.