return 0?

I ask it here even if it may not be an easy question. What is the reason of writing down return 0 in the int main() function? I find on many forums answers like: "not to forget that int main() returns 0...". I understand that int main() by default returns 0 if is successful and 1 if it fails. Can anyone give an example where the code fails when return 0 is omitted in the int main()? If is the same w/o it, then people just write it down because they don't have anything else in mind to do?

Thank you,
Claudiu
To show the program executes without error?

This is my answer without referring to anything.
main() will automatically return 0 if the program exits the function without returning. It is the only function that will do this, so often people will put it at the end anyway.
closed account (2LzbRXSz)
return 0; is just a universal way of saying your program exited without any error. In Xcode, if you stop your program using the big square button, insted of returning 0, it does return 9; (which if I remember correctly, means that your program is dependant on the compiler? Don't quote me on it though).

Some compilers don't care if you write return 0; or not, but it's like saying thank you after someone does something for you - it's the right thing to do.
Last edited on
Thank you all. I am still not convinced.
"To show the program executes without error": I guess if you have errors the int main() won't return 0 even if u write it down there.
"Some compilers don't care if you write return 0": I am not sure if return 0 is compiler dependent or not.
"but it's like saying thank you after someone does something for you - it's the right thing to do": If the scope is to keep a program as simple and short, then return 0 wouldn't it be an useless line?
Thank you for the patience,
Claudiu.
closed account (2LzbRXSz)
Well, it also marks the end of your program. Nothing after return 0; is executed. Sort of like punctuation. We can assume what someone is trying to say without it, but it is still nice if the person you are speaking with uses proper punctuation (I'm sure you've seen the post that went around years ago - it said something like "Let's eat grandma v.s. Let's eat, grandma. Punctuation saves lives")
Topic archived. No new replies allowed.