Why is return 0 optional?
Asked Answered
K

4

24

Why, if I write

int main() 
{ 
    //... 
}

do I not need to write return 0; at the end of the main function? Does the compiler do it for me?

I use GCC / C99.

Kelp answered 9/11, 2010 at 21:8 Comment(2)
Related: #204976Expatriate
You will generally want to remember your return statements. If you only write small programs for yourself, you will probably not run into problems. But if you write large programs or combine many different, it is good to have the interfaces behaving as expected. When a program returns 0, it means it finished successfully, other values indicate error. Relying on compiler / platform specific functions to keep your code working can lead to problems.Figurate
T
26

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).

Tendinous answered 9/11, 2010 at 21:15 Comment(2)
Where in the C standard it states the function main returns 0 by default if there is no explicit return statement at the end of main function it only states this reaching the } that terminates the main function returns a value of 0. the condition if there's no explicit return statement doesn't appear thereMatri
@Matri And how exactly would the program reach } if there is a return statement just before the } ?Vacillate
B
30

C99 and C++ special case the main function to return 0 if control reaches the end without an explicit return. This only applies to the main function.

The relevant bit of the C99 spec is 5.1.2.2.3 for the main special case

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 the main function is equivalent to calling the exit function with the value
returned by the main function as its argument; reaching the } that terminates the main function returns a value of 0.

6.9.1/12

If the } that terminates a function is reached, and the value of the function call is used by the caller, the behavior is undefined.

You can test this out with gcc:

int foo ( void ) { }
int main( void ) { }

C89 mode ( errors for both functions ):

sandiego:$ gcc src/no_return.c -std=c89 -Wall 
src/no_return.c: In function ‘main’:
src/no_return.c:2: warning: control reaches end of non-void function
src/no_return.c: In function ‘foo’:
src/no_return.c:1: warning: control reaches end of non-void function

C99 mode ( main is a special case ) :

sandiego:$ gcc src/no_return.c -std=c99 -Wall
src/no_return.c: In function ‘foo’:
src/no_return.c:1: warning: control reaches end of non-void function
Bigelow answered 9/11, 2010 at 21:31 Comment(0)
T
26

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).

Tendinous answered 9/11, 2010 at 21:15 Comment(2)
Where in the C standard it states the function main returns 0 by default if there is no explicit return statement at the end of main function it only states this reaching the } that terminates the main function returns a value of 0. the condition if there's no explicit return statement doesn't appear thereMatri
@Matri And how exactly would the program reach } if there is a return statement just before the } ?Vacillate
A
4

Yes. main in C is a very special function that has some extra rules. See the paragraph in the C99 standard about its termination below. In essence it says that if you quit the function without returning a value this is equivalent as if you had given a return value of 0. This is special to main, doing so with other functions where the calling function expects a return value might (and will) crash your program.

If the return type of the main function is a type compatible with int, a return from the initial call to the main function is equivalent to calling the exit function with the value returned by the main function as its argument; reaching the } that terminates the main function returns a value of 0. If the return type is not compatible with int, the termination status returned to the host environment is unspecified.

Alpenstock answered 9/11, 2010 at 21:35 Comment(0)
R
-1

Basically, yes. Functions are not required to return anything, even if they declare a return type other than void. The value returned will be undefined.

Note that C99 requires that functions that declare non-void return types always terminate by hitting a return statement. So if you were compiling using your compiler's C99 mode this code would result in a compile-time error.

Rottweiler answered 9/11, 2010 at 21:14 Comment(8)
Can you link to the appropriate part of the C99 standard to confirm this? It seems to contradict Johannes Schaub - litb's answer.Hectocotylus
I was reading from here, which claims it's part of the C99 spec. I did not actually check the spec myself. publib.boulder.ibm.com/infocenter/comphelp/v8v101/…Rottweiler
That says " Under compilation for strict C99 conformance, a function defined with a return type must include an expression containing the value to be returned." which is true in general, but the function being discussed happens to main, which is a special case.Bigelow
@Pete I do believe that page is wrong. C99 does not seem to have such a requirement: It only states that having a return statement without a value in a non-void function requires a diagnostic. It however does not state that flowing off the end of a value returning function requires a diagnostic, because C99 does not seem to anymore have the rule that a "return;" would automatically be inserted when not present, which C89 did have.Tendinous
@Johannes C99 § 6.9.1#12 says (as you have in your answer) that it is undefined behaviour for control to reach the end of a function which returns a value to a caller. The page is a bit wrong, as int foo(void) { while (true); } has no UB according to the spec, but does not meet the requirement of having a return with a value. Returning 0 from main is in C99 § 5.1.2.2.3Bigelow
@Pete note that having undefined behavior is not sufficient for not requiring a diagnostic. The C Standard can, as opposed to C++, require a diagnostic and state behavior as undefined. So if C99 would say somewhere that flowing off the end of a function is equivalent to saying return;, then the implementation needs to give a diagnostic because it would be a constraint violation (see the example in 5.1.1.3/2 of C99 TC3).Tendinous
@Johannes what exactly is wrong in what I said? You don't need a diagnostic for int foo(void) { while (true); } because it is valid; the IBM page says you have to have a return, whereas the spec says the error is control hitting the end of the function. If, as in the infinite loop, or a function which calls exit, there is nothing in the spec to say it is wrong, but IBM's page says it is an error not to have a return. Which is true in general, but not if you don't return at all, or if your function fits with the definition of main.Bigelow
@Pete There is nothing wrong with what you said. It's all correct (as far as I can see).Tendinous

© 2022 - 2024 — McMap. All rights reserved.