I’m using gcc -O -Wall -Wextra to try to help students find faults in their code. Imagine my surprise when this code, which never returns a value from main(), passes without a warning:
int main(int argc, char* argv[]){
if(argc > 2)
fprintf(stderr, "Too many arguments\n");
else if(argc == 2){
FILE* file = fopen(argv[1], "r");
if(file != NULL)
doSomethingNifty(file);
else
fprintf(stderr, "File unable to be opened\n");
}
else{
soSomethingNifty(stdin);
}
}
After trying everything else I can think of, I finally tried changing the name of the function. If I call it maim, I get what I expect:
mumble.c: In function 'maim':
mumble.c:45: error: control reaches end of non-void function
Evidently our good friends at the Free Software Foundation on the C99 Standards Committee think that my students don’t want to be warned of potential bugs in main(), but only in other functions. So my question: how do I enable this warning for main()?
I have RTFM but was not enlightened.
The return 0 is implicitly added for main in C99 and C++. The default standard (gnu90) probably also does this.
Yes, if you build with gcc -Wall -Wextra -std=c89 without a return in main, you do get the warning.