How to print "GeeksforGeeks" with empty main() in C, C++ and Java? Last Updated : 23 Jul, 2025 Comments Improve Suggest changes 23 Likes Like Report Write a program that prints "GeeksforGeeks" with empty main() function. You are not allowed to write anything in main(). C language One way of doing this is to apply GCC constructor attribute to a function so that it executes before main() (See this for details). C #include <stdio.h> /* Apply the constructor attribute to myStartupFun() so that it is executed before main() */ void myStartupFun(void) __attribute__((constructor)); /* implementation of myStartupFun */ void myStartupFun(void) { printf("GeeksforGeeks"); } int main() { } Output: GeeksforGeeks 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. C #include <stdio.h> #include <stdlib.h> int main(void) { } // _start() function void _start(void) { printf("GeeeksforGeeks"); // Call main() function int var = main(); exit(var); } Now compile this by following command gcc -nostartfiles -o file file.c Output: GeeksforGeeks C++ language 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. CPP #include <iostream> class MyClass { public: MyClass() { std::cout << "GeeksforGeeks"; } } m; int main() { } Output: GeeksforGeeks 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 CPP #include <iostream> struct Mystruct { Mystruct() { std::cout << "GeeksforGeeks"; } } obj; int main() {} Output: GeeksforGeeks 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. CPP #include <cstdio> int var = printf("GeeksforGeeks"); int main() { } 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. JAVA class Myjava { static { System.out.println("GeeksforGeeks"); } public static void main(String args[]) { } } Output: GeeksforGeeks Create Quiz Comment K kartik 23 Improve K kartik 23 Improve Article Tags : C++ Explore C++ BasicsIntroduction to C++3 min readData Types in C++6 min readVariables in C++4 min readOperators in C++9 min readBasic Input / Output in C++3 min readControl flow statements in Programming15+ min readLoops in C++7 min readFunctions in C++8 min readArrays in C++8 min readCore ConceptsPointers and References in C++5 min readnew and delete Operators in C++ For Dynamic Memory5 min readTemplates in C++8 min readStructures, Unions and Enumerations in C++3 min readException Handling in C++12 min readFile Handling in C++8 min readMultithreading in C++8 min readNamespace in C++5 min readOOP in C++Object Oriented Programming in C++8 min readInheritance in C++6 min readPolymorphism in C++5 min readEncapsulation in C++3 min readAbstraction in C++4 min readStandard Template Library(STL)Standard Template Library (STL) in C++3 min readContainers in C++ STL2 min readIterators in C++ STL10 min readC++ STL Algorithm Library3 min readPractice & ProblemsC++ Interview Questions and Answers1 min readC++ Programming Examples4 min read Like