VOOZH about

URL: https://www.geeksforgeeks.org/cpp/print-geeksforgeeks-empty-main-c/

⇱ How to print "GeeksforGeeks" with empty main() in C, C++ and Java? - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

How to print "GeeksforGeeks" with empty main() in C, C++ and Java?

Last Updated : 23 Jul, 2025
Write a program that prints "GeeksforGeeks" with empty main() function. You are not allowed to write anything in main().

C language

  1. One way of doing this is to apply GCC constructor attribute to a function so that it executes before main() (See this for details). Output:
    GeeksforGeeks
  2. In linux, just override the default definition of _start() function so that it would work as a custom startup code. See this article to understand more. Now compile this by following command
    gcc -nostartfiles -o file file.c
    Output:
    GeeksforGeeks

C++ language

  1. The idea is to create a class, have a cout statement in constructor and create a global object of the class. When the object is created, constructor is called and "GeeksforGeeks" is printed. Output:
    GeeksforGeeks
  2. The idea is to create struct and use the same logic which is discussed in above. The reason is that struct and class in C++ are exactly the same data structure except struct default to public visibility while class defaults to private visibility Output:
    GeeksforGeeks
  3. By using global variable, idea is to initialise printf() function to global variable, but it will work only in C++ language as in C language we can’t initialise variable or expression like this to global variable. Output:
    GeeksforGeeks

Java language

The idea is to use static block for printing, actually any static blocks declared outside the main() method in java is executed before the main method.
Output:
GeeksforGeeks
Comment
Article Tags:
Article Tags: