# Implicit return 0 in C

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

## 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 on other sites
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 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 on other sites
Quote:
 Original post by cozmanI 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 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 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 on other sites
Well, it gets better. Some c++ compilers have an implicit return (last assigned variable).

##### Share on other sites
Quote:
 Original post by cozmanAFAIK 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 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.

• 16
• 9
• 13
• 41
• 15