Skip to content
Related Articles

Related Articles

Print “Hello World” with empty or blank main in C++
  • Difficulty Level : Medium
  • Last Updated : 30 Aug, 2017
GeeksforGeeks - Summer Carnival Banner

Write a program in C++ that prints “Hello World”, it has a main function and body of main function is empty.

Following are two different solutions.

  • We can create a global variable and assign it return value of printf() that prints “Hello World”




    // C++ program to print something with empty main()
    #include <bits/stdc++.h>
      
    int x = printf("Hello World");
      
    int main()
    {
        // Blank
    }
  • 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”.




    // C++ program to print something with empty main()
    #include <iostream>
    using namespace std;
      
    class A {
    public:
        A() // Constructor
        {
            cout << "Hello World";
        }
    };
      
    A obj; // Create Object of class A
      
    int main()
    {
        // Blank
    }

Related article:
How to print “GeeksforGeeks” with empty main() in C, C++ and Java?

This article is contributed by Devanshu Agarwal. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Want to learn from the best curated videos and practice problems, check out the C++ Foundation Course for Basic to Advanced C++ and C++ STL Course for foundation plus STL.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.



My Personal Notes arrow_drop_up
Recommended Articles
Page :