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

25 Jul, 2017
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() */void myStartupFun(void) __attribute__((constructor));  /* implementation of myStartupFun */void myStartupFun(void){    printf("GeeksforGeeks");}  int main(){}Output:GeeksforGeeksIn 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>  int main(void){}  // _start() functionvoid _start(void){    printf("GeeeksforGeeks");      // Call main() function    int var = main();    exit(var);}Now compile this by following commandgcc -nostartfiles -o file file.cOutput:GeeksforGeeks
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() */void myStartupFun(void) __attribute__((constructor));  /* implementation of myStartupFun */void myStartupFun(void){    printf("GeeksforGeeks");}  int main(){}Output:GeeksforGeeks
#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.#include <stdio.h>#include <stdlib.h>  int main(void){}  // _start() functionvoid _start(void){    printf("GeeeksforGeeks");      // Call main() function    int var = main();    exit(var);}Now compile this by following commandgcc -nostartfiles -o file file.cOutput:GeeksforGeeks
#include <stdio.h>#include <stdlib.h>  int main(void){}  // _start() functionvoid _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.#include <iostream>  class MyClass {public:    MyClass()    {        std::cout << "GeeksforGeeks";    }} m;  int main(){}Output:GeeksforGeeksThe 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>  struct Mystruct {      Mystruct()    {        std::cout << "GeeksforGeeks";    }} obj;  int main() {}Output:GeeksforGeeksBy 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>  int var = printf("GeeksforGeeks");  int main(){}Output:GeeksforGeeks
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>  class MyClass {public:    MyClass()    {        std::cout << "GeeksforGeeks";    }} m;  int main(){}Output:GeeksforGeeks
#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#include <iostream>  struct Mystruct {      Mystruct()    {        std::cout << "GeeksforGeeks";    }} obj;  int main() {}Output:GeeksforGeeks
#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.#include <cstdio>  int var = printf("GeeksforGeeks");  int main(){}Output:GeeksforGeeks
#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.
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.
C Language
C++
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Multidimensional Arrays in C / C++
Left Shift and Right Shift Operators in C/C++
Function Pointer in C
Core Dump (Segmentation fault) in C/C++
rand() and srand() in C/C++
Vector in C++ STL
Inheritance in C++
Initialize a vector in C++ (6 different ways)
Map in C++ Standard Template Library (STL)
C++ Classes and Objects