Scooter
Scooter

Reputation: 7059

gcc/g++ are giving different response to empty main function

I am using g++ (Ubuntu/Linaro 4.6.1-9ubuntu3) 4.6.1 gcc (Ubuntu/Linaro 4.6.1-9ubuntu3) 4.6.1

If I make a cpp and c file that contains only

int main(const int argc, const char *const argv[])
{

}

and compile it with g++ -Wall test_warnings.cpp I get no warning.

If I compile it with gcc -Wall test_warnings.c I get the warning you would expect:

test_warnings.c: In function ‘main’:
test_warnings.c:4:1: warning: control reaches end of non-void function [-Wreturn-type]

The same behavior is exhibited if -Wreturn-type is used instead of -Wall.

Why isn't g++ giving me a warning that the return is missing?

Upvotes: 0

Views: 312

Answers (1)

Keith Thompson
Keith Thompson

Reputation: 263657

Because C and C++ are different languages.

In C++, reaching the end of main() without executing a return statement is equivalent to executing return 0;.

In C, as of the 1990 ISO standard, falling off the end of main() returns an undefined status to the calling environment.

C99 changed this, essentially adopting the C++ rule -- but gcc doesn't implement C99 by default. (Try compiling with -std=c99.)

In any case, it can't hurt to add a return 0; statement to the end of main().

Upvotes: 7

Related Questions