Print “Hello World” with empty or blank main in C++
Write a program in C++ that prints “Hello World”, it has a main function and body of main function is empty.
Following are three different solutions.
- We can create a global variable and assign it return value of printf() that prints “Hello World”
- We can use Constructor in C++. In the below program, we create an object of class ‘A’ outer of the main Function so object declaration time it will be call to constructor so that it will be print “Hello World”.
CPP
#include <iostream>
using namespace std;
class A {
public :
A()
{
cout << "Hello World";
}
};
A obj;
int main()
{
}
|
- We can initialize a global variable with return type of function that prints “Hello World”.
C++
#include <iostream>
int fun()
{
std::cout << "Hello World" ;
return 1;
}
int x = fun();
int main() {}
|
Related article: How to print “GeeksforGeeks” with empty main() in C, C++ and Java?
Last Updated :
14 Jun, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...