3

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?

10
  • 6
    Oh, the mysteries of main(). Surely it is because a good programmer doesn't assume that this rule should force him to treat main() different from 99.99% of the other code he writes. She included. Commented Apr 6, 2010 at 0:49
  • 2
    Hm, never thought about it like that. I guess it's sort of similar to leaving off function return types in C; I always thought that was sort of shady.
    – Maulrus
    Commented Apr 6, 2010 at 0:53
  • 2
    I don't write return 0; on principle unless non-zero is a valid return value. It makes me feel like a rebel. Commented Apr 6, 2010 at 0:58
  • So, the accepted answer is "it just looks weird"? That pretty much proves that this is a totally subjective question.
    – gnovice
    Commented Apr 6, 2010 at 1:00
  • @gnovice Are you saying it doesn't look weird?
    – asveikau
    Commented Apr 6, 2010 at 1:09

9 Answers 9

14

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.

4
  • But why would it matter which of those 2 cases it was? "A difference that makes no difference is no difference."
    – gnovice
    Commented 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. 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
    Commented 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
10

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

2
  • I'm not 100% sure but as I remember the standard says it is strictly necessary.
    – Sergio
    Commented 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;.
    – GManNickG
    Commented Apr 14, 2010 at 23:47
4

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

4

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.

7
  • 1
    Do you know of a compiler that will choke without it?
    – GManNickG
    Commented Apr 6, 2010 at 0:47
  • 2
    No, but I haven't tested every compiler in use today, either. Commented Apr 6, 2010 at 0:49
  • 7
    Eh, well if a compiler chokes on it, it's probably time to ditch the compiler.
    – GManNickG
    Commented Apr 6, 2010 at 0:50
  • 2
    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. Commented Apr 6, 2010 at 0:52
  • 2
    You 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
3

It makes behavior of the code explicit.

3
  • 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.

4
  • 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
  • 2
    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
    Commented Apr 6, 2010 at 0:48
  • 2
    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.
    – GManNickG
    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.
    – interjay
    Commented Apr 6, 2010 at 0:55
3

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.

1

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

2
  • @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. 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.
    – juan
    Commented Apr 6, 2010 at 0:49
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.