Jump to content
  • Advertisement
Sign in to follow this  
Max_Payne

Implicit return 0 in C

This topic is 5005 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
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!