Write a program to print “Hello World” with an empty main function, i.e., without writing anything inside the main() function.

The main function is the entry point of any C or C++ program, where the execution begins and ends. The main function has the following syntax:

 
The main function usually contains some statements that perform the logic of the program. In this post, we will explore some of the possible ways to print “Hello World” with an empty main function. This may seem impossible or absurd, but there are actually some clever tricks to achieve this goal in C/C++:

1. Using Constructor

One of the possible ways to print “Hello World” with an empty main function is to use constructor. A constructor is a special member function of a class that is invoked when an object of that class is created.

We can use a constructor to print “Hello World” to the std::cout or printf() function, and create an object of that class before the main function. This way, the constructor will be executed before the main function, and print “Hello World” to the standard output. For example, we can write the following code in C++:

 
Please note that we can also use std::cout or printf() inside constructor of a struct and create its object outside main() function, as shown below:

2. Using Global Variables

Another possible way to print “Hello World” with an empty main function is to use global variables and the printf() function. We can use a global variable to store the return value of the printf() function that prints Hello World, and declare it before the main function. This way, the printf() function will be executed before the main function, and print “Hello World” to the standard output. For example, we can write the following code in C:

 
In C++, we can initialize the global variable with return type of function that prints “Hello World” using std::cout.