Consider following program:
#include <iostream>
int main = ( std::cout << "C++ is excellent!\n", 195 );
Using g++ 4.8.1 (mingw64) on Windows 7 OS, the program compiles and runs fine, printing:
C++ is excellent!
to the console. main
appears to be a global variable rather than a function; how can this program execute without the function main()
? Does this code conform to the C++ standard? Is the behavior of the program is well defined? I have also used the -pedantic-errors
option but the program still compiles and runs.
gcc 4.8.1 generates the following x86 assembly:
Note that
cout
is called during initialization, not in themain
function!.zero 4
declares 4 (0-initialized) bytes starting at locationmain
, wheremain
is the name of the variable[!].The
main
symbol is interpreted as the start of the program. The behavior depends on the platform.