return 0 implicit
You refer to the C++ Standard, chapter 3.6.1 paragraph 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
executingreturn 0;
If you haven't got the Standard at hand, you can show then the paragraph in a Working Draft. Here is one for c++98, which already had this defined.
You can learn more here.
Why is return 0 optional?
The most recent C (currently that's C99 with a few amendments) returns 0
from main
by default if there is no explicit return statement at the end of the function, and control flows off the function's end (see 5.1.2.2.3 in C99 TC3). This is because most often one would write such a form of return anyway.
In C89 you need to return something there - it has no such implicit return. But the compiler is by no means required to diagnose such a mistake (see 3.6.6.4 in the C89 draft and 6.9.1/12 in C99 TC3).
What was the rationale for making `return 0` at the end of `main` optional?
In The New C Standard section 5.1.2.2.3 Program termination the author Derek Jones commentary on this lines from the C99 standard:
reaching the } that terminates the main function returns a value of 0
is:
The standard finally having to bow to sloppy existing practices.
Which indicates the rationale was to address poor programming practices with respect to explicitly returning a value from main
. Prior to this the status returned was undefined.
He indicates that many implementations already implemented this even in C90, so the fact that this change already reflected common implementation also probably helped.
Why is explicitly returning 0 from main() considered good practice?
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.
Implicit int return value of C function
Such a thing is possible, but only under the assumption that the return value of the function is never used. The C11 standard says in para 6.9.1:
If the } that terminates a function is reached, and the value of the
function call is used by the caller, the behavior is undefined.
(AFAIR previous version of the standard had similar wording)
So it would be a good idea to transform all the functions of that sort that you have to void
functions, so no user of such a function could be tempted to use the return value.
implicit return value in C with no explicit return in a function that should return non-void
I compiled with gcc version 4.9.2 on Linux with return-type warning on [-Wreturn-type]
The caller expected a true (in C, interpreted as non-zero) for success and 0 for failure. This is what was happening. Since a function that returns non-void, when called, space is created on the stack for the return value, then context is switched to the callee, and finally, when the control comes back to the caller, context is switched back popping all local variables from stack and keeping the return value there on stack to be popped by the caller after return from callee. Thus, having no explicit return statement caused some value to be returned. That is equivalent to having explicit return statement as ‘return 1;’ or 'return 0;' as appropriate. So, better (of course) would be to have explicit return statement e.g.
int somefn() {
// .. do something ..
if (..somecond..)
return 0; // failure
else
return 1; // success
}
To avoid surprises, I would say, compilers should flag 'no return statement in function returning non-void'
as error.
The function should return 1 but it is returning 0
If you had written out your braces using one of the established conventions, you would have quickly discovered that there is not an explicit return
value on all program control paths.
That means the behaviour of your code is undefined. (Note that main
is an exception to this rule, with implicit return 0;
statements added implicitly.)
You probably mean to write return turn_yeh(x, y)
on the missing branches.
Reference: https://en.wikipedia.org/wiki/Indentation_style
Can I omit return from main in C?
Yes, as of C99, reaching the }
at the end of main returns 0 if the return type of main
is compatible with int
.
5.1.2.2.3 Program termination
If the return type of the
main
function is a type compatible with int, a return from the initial call to themain
function is equivalent to calling theexit
function with the value returned by themain
function as its argument;11) reaching the}
that terminates themain
function returns a value of 0. If the return type is not compatible withint
, the termination status returned to the host environment is unspecified.
What the reasons for/against returning 0 from main in ISO C++?
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).
Related Topics
So_Rcvtime and So_Rcvtimeo Not Affecting Boost.Asio Operations
Unit Test That a Class Is Non Copyable, and Other Compile-Time Properties
Converting Steady_Clock::Time_Point to Time_T
What Is Namespace Used For, in C++
Why Is Std::Cout So Time Consuming
Multiple "Could Not Be Resolved" Problems Using Eclipse with Mingw
Dealing with Floating Point Exceptions
Use of 'Auto Func(Int)' Before Deduction of 'Auto' in C++14
Why Do I Need to Include Both the iOStream and Fstream Headers to Open a File
What Rules Does Compiler Have to Follow When Dealing with Volatile Memory Locations
Hide the Console of a C Program in the Windows Os
Stl Container with Std::Unique_Ptr's VS Boost::Ptr_Container
Google Mock - How to Call Expect_Call Multiple Times on Same Mock Object
How to Embed the Gnu Octave in C/C++ Program
Why Class Size Depend Only on Data Members and Not on Member Functions