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?

Maulrus
  • 1,661
  • 1
  • 17
  • 26
  • 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. – Hans Passant Apr 06 '10 at 00: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 Apr 06 '10 at 00: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. – Dennis Zickefoose Apr 06 '10 at 00:58
  • So, the accepted answer is "it just looks weird"? That pretty much proves that this is a totally subjective question. – gnovice Apr 06 '10 at 01:00
  • @gnovice Are you saying it doesn't look weird? – asveikau Apr 06 '10 at 01:09
  • @gnovice Well, it's the most applicable answer, I think. The non-conforming compiler answer is perhaps more factual, but I'd say that most of the code I was referring to was written by people using recent, standard compilers, making that answer less applicable. – Maulrus Apr 06 '10 at 01:14
  • @asveikau: I'm saying that "weird" is a subjective value judgment. – gnovice Apr 06 '10 at 01:15
  • @Maulrus: I find it very telling that you would pass up the "more factual" answer in favor of the one based completely on opinion. – gnovice Apr 06 '10 at 01:19
  • @gnovice: It's not as applicable in the sense that my question was talking about recent code which is more often than not written for a compiler such as VC++ or g++. – Maulrus Apr 06 '10 at 01:43
  • @gnovice - My comment was a bit concise because I was attempting to be a bit humorous. My point was that subjective-sounding judgments like "weird" are sometimes rather *objectively* so. I think the percentage of programmers who would agree with this judgment is fairly high. – asveikau Apr 06 '10 at 04:46

9 Answers9

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.

Brian R. Bondy
  • 314,085
  • 114
  • 576
  • 619
  • But why would it matter which of those 2 cases it was? *"A difference that makes no difference is no difference."* – gnovice Apr 06 '10 at 02:10
  • 2
    @gnovice: Because if you have #2 "You forgot" then maybe you would have put a return value of 1 there instead. – Brian R. Bondy Apr 06 '10 at 02: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 Apr 06 '10 at 02: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. – Brian R. Bondy Apr 06 '10 at 04: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).

cpalmer
  • 1,097
  • 6
  • 10
  • I'm not 100% sure but as I remember the standard says it is strictly necessary. – Sergio Apr 07 '10 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 Apr 14 '10 at 23:47
4
  • 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
  • 97,531
  • 20
  • 242
  • 238
  • 3
    "Some compilers may issue a warning if you don't return anything." Compiler is wrong, and you are correct in that case :) – AraK Apr 06 '10 at 00: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 Apr 06 '10 at 00: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 Apr 06 '10 at 00: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 Apr 06 '10 at 00:55
4

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

Corey
  • 2,945
  • 1
  • 14
  • 14
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.

Mark Rushakoff
  • 224,642
  • 43
  • 388
  • 389
3

It makes behavior of the code explicit.

dkamins
  • 20,091
  • 6
  • 48
  • 57
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.

Potatoswatter
  • 126,977
  • 21
  • 238
  • 404
1

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

AraK
  • 87,541
  • 35
  • 171
  • 230
  • @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 Apr 06 '10 at 00: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 Apr 06 '10 at 00: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.

Michael Burr
  • 311,791
  • 49
  • 497
  • 724