I know that the C++ standard says that return 0
is inserted at the end of main()
if no return statement is given; however, I often see recently-written, standard-conforming C++ code that explicitly returns 0 at the end of main()
. For what reasons would somebody want to explicitly return 0 if it's automatically done by the compiler?
9 Answers
By being explicit you are explicitly showing your intent.
By relying on something implicit you could have 2 cases: 1) You intended it, 2) You forgot it.
-
But why would it matter which of those 2 cases it was? "A difference that makes no difference is no difference."– gnoviceCommented Apr 6, 2010 at 2:10
-
2@gnovice: Because if you have #2 "You forgot" then maybe you would have put a return value of 1 there instead. Commented Apr 6, 2010 at 2:28
-
A value of 1 generally indicates an error or abnormal termination. The OP is specifically asking about
return 0
vs. noreturn 0
(i.e. a successful return). Forgetting to handle errors is a separate issue altogether. If I saw code without an explicitreturn 0
, I wouldn't instantly think "they completely forgot about errors".– gnoviceCommented Apr 6, 2010 at 2:48 -
@gnovice: Precisely my point. A value of 1 generally indicates an error. Anyway I don't want to argue so feel free to downvote. Commented Apr 6, 2010 at 4:19
Because it just looks weird to not "return" something from a function having a non-void return type (even if the standard says it's not strictly necessary).
-
I'm not 100% sure but as I remember the standard says it is strictly necessary.– SergioCommented Apr 7, 2010 at 22:19
-
1@Sergio: If the end of a non-void function is reached without a return statement, it leads to undefined behavior. However, main implicitly will
return 0;
. Commented Apr 14, 2010 at 23:47
Misunderstanding. There's simply no reason to, and if someone doesn't know that they'll add return 0;
.
Just because your code complies with the standard, who says your code is going to be run through a compliant compiler? Believe it or not, people do use compilers besides just recent versions of GCC and Visual C++.
And of course there's the explicit intent thing that everyone else has mentioned.
-
1
-
2No, but I haven't tested every compiler in use today, either. Commented Apr 6, 2010 at 0:49
-
7Eh, well if a compiler chokes on it, it's probably time to ditch the compiler. Commented Apr 6, 2010 at 0:50
-
2Fair, but some people in some fields (military and automotive come to mind) don't have the luxury of just updating their tools whenever they feel like it. Commented Apr 6, 2010 at 0:52
-
2You can't always ditch the compiler - you might need a specifix compiler for an old, or embedded platform. Commented Apr 6, 2010 at 0:53
Makes it clear to other programmers that you didn't just forget to put the return statement there.
Some compilers may issue a warning if you don't return anything.
Shows explicitly what the function returns.
Many programmers don't know about this rule.
-
3"Some compilers may issue a warning if you don't return anything." Compiler is wrong, and you are correct in that case :) Commented Apr 6, 2010 at 0:45
-
2Does it really matter if you are right and the compiler is wrong? In most cases compiler warnings are unacceptable even if you are "technically" right. Especially since the compiler warning is a good indication the compiler is not compliant with the implicit return 0.– SoapBoxCommented Apr 6, 2010 at 0:48
-
2I wouldn't say most. It can happen, sure, we've all seen it. But I'm willing to bet most warnings on issued on situations where the code is actually wrong. Commented Apr 6, 2010 at 0:49
-
2@GMan: I didn't say that most compiler warnings /are/ issued on correct code. I said that most warnings /can/ be issued on correct code. It may only happen in a small percentage of cases for each warning though.– interjayCommented Apr 6, 2010 at 0:55
Because some people don't know. Not necessarily the people who wrote that code (although that's also possible), but some people out there. Explicitly writing return 0;
is being nice to them.
Also, as a convention it makes the language more uniform, which aesthetically is important to at least me.
Because this is how they did it 30 years ago. It is more of a convention IMO.
-
@Juan Personally I don't write
return 0
at the end ofmain
, I am just answering the question of why do we see it although it is not necessary. Commented Apr 6, 2010 at 0:47 -
I know, all I'm saying is that doing something because that's how they did it 30 years ago is more of a reason not to do it.– juanCommented Apr 6, 2010 at 0:49
I often do it because I often compile code for straight C, so I either type it in out of habit or because the snippet I created main()
from has the explicit return. There's no reason to remove it, so it usually stays.
Then again, there are times when I won't bother typing it in (maybe I realized I didn't need it) or I may have used a different snippet.
return 0;
on principle unless non-zero is a valid return value. It makes me feel like a rebel.