If you add the -v
flag to your invocation, i.e. g++ -std=c89 -x c -O2 -Wall -pedantic -pthread -v main.cpp
, you'll notice that gcc adds some object files to your linker, i.e. crtbegin.o, crtend.o, crti.o and crtn.o, as well as crt1.o or crt0.o (it could be either.) The latter contains the entry point function (i.e., _start
) that initializes the process and calls exit(main(argc, argv))
.
Note this is actually implementation and operating system dependent (but gcc on linux was used as an example.)
In C89, if you don't specify a return value for main, the value returned is undefined.
2.1.2.2 Hosted environment
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. If the main function executes a return that specifies
no value, the termination status returned to the host environment is
undefined.
In C99, you cannot declare a function without a return type (the implicit int rule is removed.) Compared to C89, C99 is explicit about the return type of main:
It shall be defined with a return type of int and with no parameters:
[...] or in some other implementation-defined manner.
It also removed the undefined return value:
5.1.2.2.3
1 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.