Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GCC default main return value is not zero

Tags:

c

gcc

I have some C programs without any explicit return from main, like this:

int main(int argc, char *argv[])
{
  // blah blah
}

If I compile them with GCC 4.6.3 and the following options:

gcc file.c -Wall -Wextra

Programs does not return 0 and give me problems, but the standard (and a lot of other answers on SO) says:

If the return type of the main function is a type compatible with int, a return from the initial call to the main function is equivalent to calling the exit function with the value returned by the main function as its argument;[10] reaching the } that terminates the main function returns a value of 0. If the return type is not compatible with int, the termination status returned to the host environment is unspecified.

[10] In accordance with 6.2.4, the lifetimes of objects with automatic storage duration declared in main will have ended in the former case, even where they would not have in the latter.

My programs return different values, 160, 15, 14... garbage?

Am I missing something? Is this a GCC bug? Can't find anything in the GCC online doc. Is GCC still referring to some old C standard (pre-C99) where this could be not specified?

like image 965
effeffe Avatar asked Oct 17 '12 14:10

effeffe


1 Answers

By default gcc is -std=gnu89 which is C90 + GNU extensions.

And C90 says:

(C90, 5.1.2.2.3) "If the main function executes a return that specifies no value, the termination status returned to the host environment is undefined"

Compiles with -std=c99 or -std=gnu99 to have a return value of 0 when return is omitted in main function.

like image 149
ouah Avatar answered Sep 24 '22 07:09

ouah