Q:

Shortest Program in c++

I came across this code recently that compiles, but runs with segmentation fault(g++). 
Here's the original link from topcoder
topcoder profile
#include <iostream>
int main = ( std::cout << "Hello world!\n", 42  );

This also compiles
int main=0;

Can someone explain what's happening in this program. Using g++

A:

This is all silly games.  Both programs violate the requirement "a program shall contain a global function called main" (3.6.1p1).  Those programs may fool some compilers because they define a symbol main, but that symbol is not a function at all!  No wonder at least one of them crashes when the runtime tries to use that main symbol as a function.
The shortest valid C++03 program in a hosted implementation:
int main(){}

A:

Sorry, the code you posted is not a valid C++ program. A valid C++ program must have an entry point that is a function of name main in the global scope with one of the signatures dictated by the standard. The shortest valid program in C++ is:
int main(){}

A:

If you're really interested in the size of the executable the number of lines of code really isn't important, at least not to me. What matters is machine instructions and the size of the file. Here are two really great links:

A Whirlwind Tutorial on Creating Really Teensy ELF Executables for Linux.
Tiny PE (Portable Executable, the win32 and x64 executable file format).

In short, the smallest possible executable does not necessarily depend on the number of lines of code but many other things besides. This is some seriously interesting engineering, in my opinion.

