Wednesday, May 4, 2011

Invoking a function before empty main function

It's is interesting that you can call functions or print "hello world" messages in C/C++ with empty main() function.
All global variables are initialized before calling the main() function. Therefore, it could be empty.

#include <iostream>

std::ostream &out = std::cout << "print text before main" << std::endl;

void foo() { std::cout << "foo() called before main" << std::endl; }

class A
  A() { foo(); }

A obj;

// empty main function
void main()

* This source code was highlighted with Source Code Highlighter.

This code will print:

print text before main
foo() called before main

No comments:

Post a Comment