3

Possible Duplicate:
return statement vs exit() in main()

I've just read the first chapter of Accelerated C++ (seems like an awesome book), and at the end the author says

However, explicitly including a return from main is good practice.`

Why is this considered good practice? In C99, I always omitted the return 0, using exit() to signal abnormal program termination, and never missed the explicit return.

Community
  • 1
  • 1
helpermethod
  • 59,493
  • 71
  • 188
  • 276

5 Answers5

8

A couple of reasons,

firstly, main is declared to return int, and so it should

secondly, and maybe more importantly for C++ an exit() from main will skip calling destructors for local object instances in main.

4

In C99 and in C++ if execution of the program reaches the closing brace of the main() function then an implicit return 0; is executed. That wasn't the case in C90 - reaching the end of main() without an explicit return would result in an indeterminate value being returned (strictly speaking, the behavior is undefined).

I can only guess that the authors of "Accelerated C++" feel that the explicit return is good practice simply because it makes your intent explicit. The only other reason I can think of is that it makes code compatible with C90, but I find it difficult to believe that that would hold much weight as a reason.

Michael Burr
  • 333,147
  • 50
  • 533
  • 760
  • Correct me if I'm wrong, but I thought that prior to C99, a function with a declared return type could *legally* return without specifying a value if it could be sure (based e.g. upon parameters) that the caller would never try to use the return value. Not to say that anyone *should* ever write code like that, but I think prior to C99 such code was legal and did not invoke UB. – supercat Jul 02 '13 at 21:04
1

The calling program (usually a OS shell) can gather the return code and know if the program failed or not.

Having a single exit point from a program makes a good place to put a breakpoint.

Having a single execution flow makes it easier to follow a program if you're unfamiliar with it.

Mark Ransom
  • 299,747
  • 42
  • 398
  • 622
  • 3
    #1 doesn't matter. Leaving main without explicitly returning anything is to implicitly return `0`. – sbi Nov 15 '10 at 23:52
  • As to the other two, you can't use `return 0;` if you also want to have a single exit point as well as indicate abnormal termination. – visitor Nov 16 '10 at 11:19
  • @visitor, you can have a variable that tracks the program status and return that. – Mark Ransom Nov 16 '10 at 20:17
1

I think this rule of thumb not so much applies to callling exit() instead, but to fall off main() without returning anything, relying on the implicit return 0 the run-time system has to do in that case.

I guess that main() is the only function returning a value where you can omit explicitly returning a value. IMO that's a very good reason to not to rely on that rule.

sbi
  • 219,715
  • 46
  • 258
  • 445
-1

By C++ standard main() should return int. This is an error code application returns on its termination and in practice this is useful when calling process wants to know whether this application terminated with success (usually error code is 0 in this case) or it failed.

I'll give a Microsoft specific example, but it shows general need for returning error code: ProcessA needs to create ProcessB and wait for it to terminate, after which it wants to check whether ProcessB executed successfully. ProcessA will use CreateProcess function to create ProcessB, then will use ProcessB's handle to wait for its termination and then will use GetExitCodeProcess function to get ProcessB termination code - which is int value returned from main().

Bojan Komazec
  • 9,216
  • 2
  • 41
  • 51