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).
#include <stdio.h>/* Apply the constructor attribute to myStartupFun()so that it is executed before main() */voidmyStartupFun(void) __attribute__((constructor));/* implementation of myStartupFun */voidmyStartupFun(void){printf("GeeksforGeeks");}intmain(){}chevron_rightfilter_noneOutput:
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.
#include <stdio.h>#include <stdlib.h>intmain(void){}// _start() functionvoid_start(void){printf("GeeeksforGeeks");// Call main() functionintvar = main();exit(var);}chevron_rightfilter_noneNow 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.
#include <iostream>classMyClass {public:MyClass(){std::cout <<"GeeksforGeeks";}} m;intmain(){}chevron_rightfilter_noneOutput:
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
#include <iostream>structMystruct {Mystruct(){std::cout <<"GeeksforGeeks";}} obj;intmain() {}chevron_rightfilter_noneOutput:
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.
#include <cstdio>intvar =printf("GeeksforGeeks");intmain(){}chevron_rightfilter_noneOutput:
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.
class Myjava { static { System.out.println("GeeksforGeeks"); } public static void main(String args[]) { } } |
Output:
GeeksforGeeks
This article is contributed by Viki and improved by Shubham Bansal. 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.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Print "Hello World" with empty or blank main in C++
- Is it fine to write "void main()" or "main()" in C/C++?
- C/C++ program for calling main() in main()
- Difference between "int main()" and "int main(void)" in C/C++?
- What does main() return in C and C++?
- Can main() be overloaded in C++?
- Executing main() in C/C++ - behind the scene
- Functions that are executed before and after main() in C
- How to change the output of printf() in main() ?
- How to write a running C code without main()?
- return statement vs exit() in main()
- map::empty() in C++ STL
- set::empty() in C++ STL
- unordered_map empty in C++ STL
- match_results empty() in C++ STL
- array::empty() in C++ STL
- multimap empty() function in C++ STL
- unordered_multiset empty() function in C++STL
- unordered_multimap empty() function in C++ STL
- unordered_set empty() function in C++ STL

