• Advertisement
Sign in to follow this  

Implicit return 0 in C

This topic is 4739 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Just a quick question. Is implicit return 0 part of ANSI C? If so, since when? And was it part of K&R C? Could someone also please provide a link to information about that, I can't seem to find one on google. Thanks.

Share this post


Link to post
Share on other sites
Advertisement
dunno. try compiling and running this on an ANSI C compliant compiler (i may not have all the right headers):


#include <stdio.h>

void fake()
{
return;
}

int main(void)
{
if ( fake() == 0 )
{
printf("There is an implicit return 0\n");
} else {
printf("Someone lied to you\n");
}

return 1;
}



Share this post


Link to post
Share on other sites
I don't think that's what the OP meant.


AFAIK there is no implicit return 0 in ANSI C, there is however in C++.

int main()
{
}

//returns 0 in ANSI C++, not specified in ANSI C

edit: example was bad, main only

Share this post


Link to post
Share on other sites
Quote:
Original post by cozman
I don't think that's what the OP meant.


AFAIK there is no implicit return 0 in ANSI C, there is however in C++.


int func()
{
}

assert(func() == 0); //ANSI C++, not ANSI C



I'm asking this because I lost marks in a C exam when asked if some C program would compile, and it had no return statement. So I'd like to know (for sure) if implicit return 0 is part of standard C, and if so, if its really part of all standards. I don't really care if it compiles on a particular compiler, I want to know about the multiple C standards in regard to this. Please note that I'm considering the main() function here. In case theres some weird thing in ANSI C about implicit return 0 in main only.

Share this post


Link to post
Share on other sites
On the intel x86 architecture, I'm pretty sure you would end up with a return value of whatever was lying around in ax or eax depending on the size of int for the target machine. I haven't verified this however. You may get different results in a debug build, as extra debug code might set returns to zero for debug purposes.

Share this post


Link to post
Share on other sites
As far as I know it will compile, it may have undefined results, but it will compile on any compiler I've ever used. You may get a warning (on gcc, I get a warning with -Wall) but it still compiles.

Share this post


Link to post
Share on other sites
Well, it gets better. Some c++ compilers have an implicit return (last assigned variable).

Share this post


Link to post
Share on other sites
Quote:
Original post by cozman
AFAIK there is no implicit return 0 in ANSI C, there is however in C++.

Are you sure? Certainly main implicitly returns 0 at end-of-control, but I had never heard anything like this in general (and for the record, my compiler most certainly does not produce a 0).

Share this post


Link to post
Share on other sites
Oops, I knew that, I changed my original example as soon as I realized I was wrong but looks like Max_Payne read my reply before I changed it.

Share this post


Link to post
Share on other sites
Visual Studio C++ only reports a warning. The code runs but the function returns garbage. I guess it is similar to uninitialized variables.

Share this post


Link to post
Share on other sites
Allright, so its the case that theres an implicit return 0 in main in C99, but what about older C standards? Specifically older ANSI standards and the old K&R standard.

Share this post


Link to post
Share on other sites
I've never heard of it being part of a standard but the old Metrowerks compiler for the PS1 did implicitly add the return of zero, which caused a few folks some grief:)

Cheers
Chris

Share this post


Link to post
Share on other sites
Should just be main()...

But I really hate questions like that on school tests:

"Will the following compile?"


What revision of C? ANSI? 89? 99?

What compiler? What revision of that compiler?


Most people that say they code "C" still use a C++ compiler....

And lots of things will compile, they just don't conform to standards(any)... lots of programmers code things that compile and run fine.. but rely on functions using "undefined behavior"...




If anything the question should be "Does this code conform to XXXX standard?"


Getting back to the question at hand:

Section 6.6.6.4 of ANSI/ISO 9899-1990 contains the Semantic restriction

> If a return statement without an expression is executed, and the value
> of the function call is used by the caller, the behavior is undefined.

It goes on to say:

> Reaching the } that terminates the function is equivalent to executing
> a return statement without an expression.


So it does compile, and should. You essentially get a return; insterted into your code.. the only thing is that if you use the value returned from a function doing so you don't know what your going to get.




Share this post


Link to post
Share on other sites
The standard special-cases main(), saying that if it doesn't return a value, the assumed return value is 0. Note that this case means main() returns void.

Share this post


Link to post
Share on other sites
Hunt down the all the standards documents and look for yourself.

Why they do it, I have no idea...seems stupid to me.

Share this post


Link to post
Share on other sites
Bjarne Stroustrup on C++:
Quote:
In C++, main() need not contain an explicit return statement. In that case, the value returned is 0, meaning successful execution. For example:

#include<iostream>

int main()
{
std::cout << "This program returns the integer value 0\n";
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement