Implicit return 0 in C
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.
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;}
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
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
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.
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.
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.
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).
Implicit return 0 is applicable to main only, cozman & Palidine.
Comeau TechTalk: Must main return a value?
GCC Project: Patches for C99 main semantics.
It holds for both C and C++.
Comeau TechTalk: Must main return a value?
GCC Project: Patches for C99 main semantics.
It holds for both C and C++.
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.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement