Can you print anything in C++, before entering into the main function?
It is interview question in Bloomberg:
Answer :create a global varia
#include
struct X
{
X()
{
std::cout << "Hello before ";
}
} x;
int main()
{
std::cout << "main()";
}
This well-formed C++ program prints
Hello before main()
You see, the C++ standard guarantees that the constructors of namespace-scope variables (in this example, it's x) will be executed before main(). Therefore, if you print something in a constructor of such an object, it will be printed before main(). QED