5

I know that the current C++ standard special cases main so that falling off the end has the same effect as return 0; rather than undefined behavior.

I was recently surprised to see an example at codereview where the responses not only pointed out that including the final return 0; is optional, but actually went so far as to remove it from the original code.

Thus prompts my question — is it really considered bad style to make the return 0; explicit at the end of main? What is the rationale for or against?

Community
  • 1
  • 1
  • 2
    possible duplicate ? http://stackoverflow.com/questions/204476/what-should-main-return-in-c-and-c – KostasRim Aug 29 '16 at 10:05
  • 9
    removing it from the code is (a) ridiculous and (b) undesirable behaviour in a production environment. Every time we touch code we have the chance to introduce a bug. Code should be touched when necessary, not because it's not presented the way we personally like. – Richard Hodges Aug 29 '16 at 10:10
  • 1
    @KostasRim: That doesn't answer my question. (and, in fact, the top answer explicitly punts on my question) –  Aug 29 '16 at 10:11
  • I think this is a matter of style and opinion. Some would say to put it in so that the successful return is explicit. Some would say to leave it off for brevity. Just pick one and stick to it for your own projects, but be happy to go along with conventions for whatever project you're working on. – TartanLlama Aug 29 '16 at 10:12
  • 2
    code style is a lot of times a thing of opinion. I usually add the return to avoid confusion with people who don't know about this. Also I prefer using the [EXIT_SUCCESS, EXIT_FAILURE](http://en.cppreference.com/w/cpp/utility/program/EXIT_status) makros so I avoid hard-coding return codes. – Hayt Aug 29 '16 at 10:18
  • My personal opinion is that dropping off the end of main is appalling style - but I think this is purely a matter of opinion. – Martin Bonner supports Monica Aug 29 '16 at 10:25
  • 1
    If the `main` doesn't return anything meaningful then I don't gratuitously add it (why bother? why give the impression you have something meaningful to return when you don't?). If the `main` returns success or failure then I always use `EXIT_SUCCESS/EXIT_FAILURE`. I would never add it to a trivial code example where your only focus should be the problem being discussed. – Galik Aug 29 '16 at 10:27
  • 1
    It's purely subjective, but here's one personal opinion: The purpose of code is to communicate with *humans*. The computery bits are just a by-product. Considerations of style and presentation should primarily optimize for the human response. That said, I have *never* seen anyone confused by the semantics of `main`, whether you have an explicit return or not. So purely from the concern of understandability, I see no evidence that saying `return` explicitly improves clarity or reduces surprise. Leave it in if you like it, or don't say it if you think less code means less stuff to look at. – Kerrek SB Aug 29 '16 at 11:49
  • Keep in mind the code in question is tagged 'programming-challenge'. There's no production code to see here. That's relevant for the suggestions given in the answers. – Mast Aug 29 '16 at 13:46

2 Answers2

4

I'll take a wild stab in the dark here and say that people fall into two camps:

  • those who remove the line think it is redundant and all such code should be removed for brevity

  • those who add the line think it makes the return value clear and unambiguous to lesser coders.

Personally, I would tend to always write a meaningful return statement in main in my production code (if only because my production mains tend to also contain code paths that end up returning something other than 0, generally in exception handlers), although I wouldn't bother for a trivial main that never returns anything else; for example, I don't think I've ever done so in, say, a Coliru post for a Stack Overflow demonstration.

Some would say that it's absurd to alter a codebase to flip between these two states, that the arguments are both very weak in the grand scheme of things, and that such a personal choice is not worth risking the introduction of bugs.

But I'd say this depends almost entirely on what your environment is like. If you're halfway through a release cycle, of course you're going to make code maintenance improvements and style adjustments: this is the best time to avoid accruing technical debt, and you absolutely want to do that. But if you're planning to make this change directly on a production server, or in version control one week before that big release, you're out of your mind.

(Hopefully your policies prevent such madness anyway. Code freeze, yes? No changes to production, right?)

So, although it goes without saying that the underlying choice is highly subjective, we can quantify the risk/benefit of enforcing such a choice after-the-fact.

But never mind real life, what about on Code Review? Well, I have no idea; you'd have to ask them. Personally, on those specific examples, I probably would have removed it too, albeit with the written caveat that this were purely a style choice. Whether purely style changes are appropriate on Code Review is a question for Code Review Meta.

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
0

From C++, 3.6.1 Main Function

(3.6.1/5) An implementation shall not predefine the main function. This function shall not be overloaded. It shall have a return type of type int, but otherwise its type is implementation-defined. All implementations shall allow both of the following definitions of main:

int main() { /* ... */ } and

int main(int argc, char* argv[]) { /* ... */ }

So in C++, two prominent signatures for main function are : int main() and int main(int argc, char** argv)

Further in C++, 3.6.1 Main function

(3.6.1/5) A return statement in main has the effect of leaving the main function (destroying any objects with automatic storage duration) and calling exit with the return value as the argument. If control reaches the end of main without encountering a return statement, the effect is that of executing return 0;

So, although the signature suggest that function should return some integer, it is not necessary to return 0. By default, C++ considers return value to be 0.

Advantages:
- Treating main as normal function and following similar C++ coding convention
- return 0 explicitly specify normal return value, in other cases we can return non-zero value

Although, all these points are matter of coding convention. It is still choice of programmer!

Rishit Sanmukhani
  • 2,159
  • 16
  • 26
  • More like 3 proeminent signatures actually : `int main(int argc, const char **argv, const char **env)` –  Aug 29 '16 at 10:14
  • 4
    @Ascam There're only two, according to the standard. – songyuanyao Aug 29 '16 at 10:20
  • @songyuanyao I've searched for it then, there are actually **three** standard prototypes for C++ `main`. The third one being `int main (int argc, char *argv[] , other_parameters )` where `other_parameters` is described as : "*Implementations may allow additional forms of the main function as long as the return type remains int. A very common extension is passing a third argument of type char*[] pointing at an array of pointers to the execution environment variables.*". [See on CppReference](http://en.cppreference.com/w/cpp/language/main_function) –  Aug 29 '16 at 10:26
  • 1
    Technically of course, it depends which standard you are talking about. The C++ standard only blesses two signatures. Posix however adds a third. – Martin Bonner supports Monica Aug 29 '16 at 10:29
  • Ok, sorry, my bad :) –  Aug 29 '16 at 10:31
  • My question isn't about the signature or the legality of omitting the return statement.... –  Aug 29 '16 at 10:42
  • True, I added the details regarding the signatures and return statement for clarification (as reference to some comments above). In addition, I have added my view point regarding `return 0` statement. – Rishit Sanmukhani Aug 29 '16 at 10:44
  • I don't really see that this addresses the question. – Lightness Races in Orbit Aug 29 '16 at 10:50
  • Questions asks rationale for/against `return 0`, and I just presented mine. Although, I still maitain, it is highly subjective. – Rishit Sanmukhani Aug 29 '16 at 10:53
  • 3
    The vast majority of this answer merely repeats the premise of the question. The only part of this that comes close to answering the question is _"advantages: treating `main` as normal function and following similar C++ coding convention. It is choice of programmer"_ Not exactly meaty... – Lightness Races in Orbit Aug 29 '16 at 10:56