On the following program, GCC (4.4 to 4.7 at least) invoked with -std=c99 -Wreturn-type doesn't give a warning about the missing return statement. #include <stdio.h> static int i = 0; int main (void) { if (i++ == 0) printf ("%d\n", main ()); } According to the C99 rules, it seems that the return statement is optional only for program termination. Though this is ambiguous, someone else at least has the same interpretation as me: http://groups.google.com/group/comp.std.c/msg/c2f56fecfb699952 Before seeing this message, I posted http://groups.google.com/group/comp.std.c/browse_thread/thread/0187ef7b23bedf16 to comp.std.c (Subject: main function without a return statement in C99/C11). Also, I think that the warning should be given in every case for the following reasons: * A missing return statement may be unintentional (I think that implicit values like here should be discouraged in general, and that the C99 rule is there more to avoid undefined behavior than to save space). * Compatibility with C90 and with freestanding environments. * It is difficult to guarantee that main() will not be called from another C file.
Neither -Wmain warns about this... I think it is a bug, at least in the case of -Wmain.
(In reply to comment #1) > Neither -Wmain warns about this... I think it is a bug, at least in the case of > -Wmain. Why? -Wmain checks the type of main, not whether it has a redundant 'return 0;' as the last statement.
(In reply to comment #2) > Why? -Wmain checks the type of main, not whether it has a redundant 'return 0;' > as the last statement. You are right, I misread the description. So this is not a bug because Wreturn-type says that it specifically does not check main?
I think -Wreturn-type says in C++ it doesn't warn about declaring: main() {} with no return type. That only applies to C++, so isn't relevant here. IMHO this isn't a bug because in C99 it's well-defined what happens if you fall off the end of main, so it would annoy people to issue a warning on perfectly valid code whenever -Wall is enabled. But then I rarely use C99 so I don't really care. If anyone suggested warning for C++ programs that fall off the end of main I would be upset :) The fact people might call main() from other functions is a sort-of-convincing argument to warn about it (calling main isn't allowed in C++).
(In reply to comment #4) > IMHO this isn't a bug because in C99 it's well-defined what happens if you fall > off the end of main, Only at program termination (if my interpretation of C99 is correct). Please read again the bug report and the links. > so it would annoy people to issue a warning on perfectly valid code > whenever -Wall is enabled. Well, GCC has various warnings on perfectly valid code, just because the code is suspicious. This is the case here too. And there are potential portability problems (not everyone may use a C99 compiler). So, good reasons to issue a warning.
(In reply to comment #5) > (In reply to comment #4) > > IMHO this isn't a bug because in C99 it's well-defined what happens if you fall > > off the end of main, > > Only at program termination (if my interpretation of C99 is correct). Please > read again the bug report and the links. I did. Please read the last sentence of comment 4. > > so it would annoy people to issue a warning on perfectly valid code > > whenever -Wall is enabled. > > Well, GCC has various warnings on perfectly valid code, just because the code > is suspicious. This is the case here too. Yes, and in each case some people want it and some don't. I'm pointing out to Manu the reasons not everyone wants the warning. Your opinion isn't the only valid one. > And there are potential portability > problems (not everyone may use a C99 compiler). So, good reasons to issue a > warning. Er, if you want to find portability problems for people not using C99 then don't use -std=c99. Then -Wreturn-type warns about main.
Changing to "enhancement" since the current behaviour is by design, not a bug.
(In reply to comment #6) > Yes, and in each case some people want it and some don't. I'm pointing out to > Manu the reasons not everyone wants the warning. Your opinion isn't the only > valid one. Then the solution would be to split -Wreturn-type into two different warnings. > Er, if you want to find portability problems for people not using C99 then > don't use -std=c99. Then -Wreturn-type warns about main. There are several reasons one may want to use -std=c99, e.g. to be able to use C99 features when available (via autoconf and/or preprocessor tests).
Confirmed.
*** Bug 70958 has been marked as a duplicate of this bug. ***
(In reply to Vincent Lefèvre from comment #8) > (In reply to comment #6) > > Er, if you want to find portability problems for people not using C99 then > > don't use -std=c99. Then -Wreturn-type warns about main. > > There are several reasons one may want to use -std=c99, e.g. to be able to > use C99 features when available (via autoconf and/or preprocessor tests). In any case, there does not seem to be a -std value to say that the program must be valid for all C90, C99, C11 and C17 standards (and the future C23 standard). That's what portability is about.
Because GCC is primarily a compiler, not a linter for portability problems.
And intersection of all the standards is quite hard to define. So, adding -std=intersection_of_all_c_standards is just not a good idea, any time same construct changes meaning between different standard you need to decide what to do. This is something that should be solved with warnings or external tools and on the warning side we already have various warnings.
Anyway, as I said initially, the warning would be interesting even in C99+ mode, because the lack of a return statement may be unintentional. For instance, the developer may have forgotten a "return err;".
But much more often it is intentional than unintentional.
(In reply to Jakub Jelinek from comment #15) > But much more often it is intentional than unintentional. That's the same thing for many kinds of warnings.
Yeah, but warnings with high false positivity rates at least shouldn't be in -Wall.
(In reply to Jakub Jelinek from comment #17) > Yeah, but warnings with high false positivity rates at least shouldn't be in > -Wall. Well, there already is -Wunused, which is included in -Wall (such warnings may typically be emitted due to #if and also in temporary code when debugging), and -Wsign-compare in C++. Anyway, there is a first issue: the warning is inexistent, even with -Wextra. There is a second issue: the warning is not emitted with -Wreturn-type when there is a call to main(). Solving these two issues alone would not yield a high false positivity rate with -Wall. (That said, I think that developers should be encouraged to have an explicit "return" for main(); in particular, this is really easy to do and improves the code readability, specially knowing the difference of behavior with other languages, such as shell scripts and Perl.)