I'm using gcc 3.4.5, and I was looking into a weird condition. The following program compiles OK: #include <stdio.h> int alloc() { return 0; } int main(int argc, char* argv[]) { printf("Testing!\n"); if(alloc() == 0)goto Radhe; return 0; Radhe: printf("Hello\n"); } The result is a program which prints "Testing", then "Hello", and then exits. I and a few other people expect that the compiler would generate an error during compile, because main should return an int, and not all code paths have a return statement; however, both gcc and msvc are compiling this OK. added note: this bug was recently discussed on the NTDEV mailing list.
Created attachment 13571 [details] The file from -save-temps
Created attachment 13572 [details] the assembly (.s) file
Reading specs from /usr/lib/gcc/i386-redhat-linux/3.4.5/specs Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --enable-shared --enable-threads=posix --disable-checking --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-java-awt=gtk --host=i386-redhat-linux Thread model: posix gcc version 3.4.5 20051201 (Red Hat 3.4.5-2)
[pinskia-laptop:~/src] pinskia% gcc -Wreturn-type t.c t.c: In function 'main': t.c:15: warning: control reaches end of non-void function This code is only undefined at runtime which means you cannot error out.
Both the C99 standard (section 5.1.2.2.3) and the C++ standard (3.6.1/5) say that if you hit the end of the main() function, then this is equivalent to a "return 0;" statement. W.
(In reply to comment #5) > Both the C99 standard (section 5.1.2.2.3) and the C++ standard (3.6.1/5) > say that if you hit the end of the main() function, then this is equivalent > to a "return 0;" statement. C90 is different from both of those and it is just undefined behavior there.