Must return 0; should be avoided in main ()?

I know that the current standard C ++ cases for standard main cases, so falling from the end has the same effect as return 0; , not undefined.

Recently, I was surprised to see an example in codereview , in which the answers not only indicated that the inclusion of the final return 0; is optional, but actually went as far as removing it from the source code.

Thus, a request is issued to my question - is it considered invalid to make return 0; explicit at the end of the main? What is the reason or against?

+5
source share
2 answers

I will take a wild blow in the dark and say that people fall into two camps:

  • those who delete the line consider it redundant, and all such code should be removed for brevity

  • those who add the string think that it makes the return value understandable and unambiguous for smaller coders.

Personally, I would always like to write a meaningful return in main in my production code (if only because my production main tends to also contain code codes that ultimately return something other than 0 , usually in exception handlers) , although I would not bother with the trivial main , which never returns anything; for example, I don’t think I have ever done this, for example, in the Coliru column for a demonstration.

Some will say that it is absurd to change the code base to switch between these two states, that the arguments are very weak in the great scheme of things, and that such a personal choice is not worth the risk of introducing errors.

But I would say that it almost entirely depends on your environment. If you are halfway to the release cycle, of course, you are going to improve the improvements in code content and style settings: this is the best time to avoid the accumulation of technical debt, and you absolutely want to do it. But , if you plan to make this change directly on the production server or in version control one week before this big release, you are crazy.

(I hope your politicians prevent this kind of madness anyway. The code freezes, right? No production changes, right?)

So, although it goes without saying that the main choice is very subjective , we can quantify the risk / benefit to ensure such a choice after the fact.

But don't think about real life, but what about a code review? Well, I have no idea; you should ask them. Personally, with these specific examples, I probably would have deleted it too, if only with a written warning that it was a pure choice of style. Whether pure style changes are appropriate for Code Review is the question for Meta Code Review.

+4
source

From C ++, 3.6.1 Main Function

(3.6.1 / 5) Implementation should not predetermine the main function. This function should not be overloaded. It must have a return type of int, but otherwise its type is determined by the implementation. All implementations must allow both of the following definitions of main:

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

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

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

Further in C ++, 3.6.1 Main Function

(3.6.1 / 5) The return statement basically has the effect of the main function (destroying any objects with automatic storage time) and calling exit with the return value as an argument. If the control reaches the end of the main one without encountering a return operator, the Effect is the result of execution 0 ;

So, although the signature suggests that the function should return some integer, there is no need for return 0 . By default, C ++ considers the return value to be 0.

Benefits:
- Processing main as a normal function and the subsequent similar agreement on coding in C ++
- return 0 explicitly indicate the normal return value, in other cases we can return a nonzero value

Although all these points are subject to coding. This is still the programmer's choice!

+1
source

All Articles