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.
From 3.6.1/1:
From this it looks like g++ happens to allow a program (presumably as the "freestanding" clause) without a main function.
Then from 3.6.1/3:
So here we learn that it's perfectly fine to have an integer variable named
main
.Finally if you're wondering why the output is printed, the initialization of the
int main
uses the comma operator to executecout
at static init and then provide an actual integral value to do the initialization.I've tried this on a Win7 64bit OS using VS2013 and it compiles correctly but when I try to build the application I get this message from the output window.
That is an ill-formed program. It crashes on my test environment, cygwin64/g++ 4.9.3.
From the standard:
The reason I believe this works is that the compiler does not know it is compiling the
main()
function so it compiles a global integer with assignment side-effects.The object format that this translation-unit is compiled into is not capable of differentiating between a function symbol and a variable symbol.
So the linker happily links to the (variable) main symbol and treats it like a function call. But not until the runtime system has run the global variable initialization code.
When I ran the sample it printed out but then it caused a seg-fault. I assume that's when the runtime system tried to execute an int variable as if it were a function.
Before going into the meat of the question about what is going on, it is important to point out that program is ill-formed as per defect report 1886: Language linkage for main():
The most recent versions of clang and gcc makes this an error and the program will not compile (see gcc live example):
So why was there no diagnostic in older versions of gcc and clang? This defect report did not even have a proposed resolution until late 2014 and so this case was only very recently explicitly ill-formed, which requires a diagnostic.
Prior to this, it seems like this would be undefined behavior since we are violating a shall requirement of the draft C++ standard from section
3.6.1
[basic.start.main]:Undefined behavior is unpredictable and does not require a diagnostic. The inconsistency we see with reproducing the behavior is typical undefined behavior.
So what is the code actually doing and why in some cases does it produce results? Let's see what we have:
We have
main
which is an int declared in the global namespace and is being initialized, the variable has static storage duration. It is implementation defined whether the initialization will take place before an attempt to callmain
is made but it appears gcc does do this before callingmain
.The code uses the comma operator, the left operand is a discarded value expression and is used here solely for the side effect of calling
std::cout
. The result of the comma operator is the right operand which in this case is the prvalue195
which is assigned to the variablemain
.We can see sergej points out the generated assembly shows that
cout
is called during static initialization. Although the more interesting point for discussion see live godbolt session would be this:and the subsequent:
The likely scenario is that the program jumps to the symbol
main
expecting valid code to be there and in some cases will seg-fault. So if that is the case we would expect storing valid machine code in the variablemain
could lead to workable program, assuming we are located in a segment that allows code execution. We can see this 1984 IOCCC entry does just that.It appears we can get gcc to do this in C using (see it live):
It seg-faults if the variable
main
is not const presumably because it is not located in an executable location, Hat Tip to this comment here which gave me this idea.Also see FUZxxl answer here to a C specific version of this question.
You are doing tricky work here. As main( somehow) could declared to be integer. You used list operator to print message & then assign 195 to it. As said by someone below, that it doesn't comfort with C++, is true. But as compiler didn't find any user defined name, main, it didn't complaint. Remember main is not system defined function, its user defined function & thing from which program starts executing is Main Module, not main(), specifically. Again main() is called by startup function which is executed by loader intentionally. Then all of your variables are initialized, & while initializing it output like that. That's it. Program without main() is ok, but not standard.