tags:

views:

188

answers:

9

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?

+3  A: 

It makes behavior of the code explicit.

dkamins
+1  A: 

Because this is how they did it 30 years ago. It is more of a convention IMO.

AraK
That's not a good reason IMO
Juan Manuel
@Juan Personally I don't write `return 0` at the end of `main`, I am just answering the question of why do we see it although it is not necessary.
AraK
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.
Juan Manuel
+9  A: 

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.

Brian R. Bondy
But why would it matter which of those 2 cases it was? *"A difference that makes no difference is no difference."*
gnovice
@gnovice: Because if you have #2 "You forgot" then maybe you would have put a return value of 1 there instead.
Brian R. Bondy
@Brian: A value of 1 generally indicates an error or abnormal termination. The OP is specifically asking about `return 0` vs. no `return 0` (i.e. a successful return). Forgetting to handle errors is a separate issue altogether. If I saw code without an explicit `return 0`, I wouldn't instantly think "they completely forgot about errors".
gnovice
@gnovice: Precisely my point. A value of 1 generally indicates an error. Anyway I don't want to argue so feel free to downvote.
Brian R. Bondy
+2  A: 
  • 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.

interjay
"Some compilers may issue a warning if you don't return anything." Compiler is wrong, and you are correct in that case :)
AraK
@AraK: Most compiler warnings can happen on correct code.
interjay
Does 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.
SoapBox
@interjay: I 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.
GMan
@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.
interjay
+3  A: 

Misunderstanding. There's simply no reason to, and if someone doesn't know that they'll add return 0;.

Bruce
+2  A: 

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.

Potatoswatter
+2  A: 

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.

Mark Rushakoff
Do you know of a compiler that will choke without it?
GMan
No, but I haven't tested every compiler in use today, either.
Mark Rushakoff
Eh, well if a compiler chokes on it, it's probably time to ditch the compiler.
GMan
Fair, 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.
Mark Rushakoff
You can't always ditch the compiler - you might need a specifix compiler for an old, or embedded platform.
Lucas Jones
I agree, sometimes you "need" an old compiler. But I think that's arguing for a corner case, and doesn't apply as a reason for the general programmer.
GMan
I agree with @GMan on this one. Respecting non-compliant compilers takes a lot of effort and yields little back. Do it if you must; otherwise don't.
wilhelmtell
+6  A: 

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).

cpalmer
I'm not 100% sure but as I remember the standard says it is strictly necessary.
Sergio
@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;`.
GMan
A: 

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.

Michael Burr