Write a C language code that prints GeeksforGeeks without any main function.
Logically it’s seems impossible to write a C program without using a main() function. Since every program must have a main() function because:-
- It’s an entry point of every C/C++ program.
- All Predefined and User-defined Functions are called directly or indirectly through the main.
Therefore we will use preprocessor(a program which processes the source code before compilation) directive #define with arguments to give an impression that the program runs without main. But in reality it runs with a hidden main function. Let’s see how the preprocessor doing their job:-
Hence it can be solved in following ways:-
- Using a macro that defines main
#include<stdio.h>#define fun mainintfun(void){printf("Geeksforgeeks");return0;}chevron_rightfilter_noneOutput: Geeksforgeeks
- Using Token-Pasting Operator
The above solution has word ‘main’ in it. If we are not allowed to even write main, we can use token-pasting operator (see this for details)#include<stdio.h>#define fun m##a##i##nintfun(){printf("Geeksforgeeks");return0;}chevron_rightfilter_noneOutput: Geeksforgeeks
-
Using Argumented Macro
#include<stdio.h>#define begin(m,a,i,n) m##a##i##n#define start begin(m,a,i,n)voidstart() {printf("Geeksforgeeks");}chevron_rightfilter_noneOutput: Geeksforgeeks
-
Modify the entry point during compilation
#include<stdio.h>#include<stdlib.h>// entry point functionintnomain();void_start(){// calling entry pointnomain();exit(0);}intnomain(){puts("Geeksforgeeks");return0;}chevron_rightfilter_noneOutput: Geeksforgeeks
Compilation using command :
gcc filename.c -nostartfiles
(nostartfiles option tells the compiler to avoid standard linking)Explanation:
Under normal compilation the body of _start() will contain a function call to main() [ this _start() will be appended to every code during normal compilation], so if that main() definition is not present it will result in error like “In function `_start’:
(.text+0x20): undefined reference to `main’.
In the above code what we have done is that we have defined our own _start() and defined our own entry point i.e nomain()- This method is contributed by Aravind Alapati
Refer Executing main() in C – behind the scene for another solution.
References:
Macros and Preprocessors in C
This article is contributed by Abhay Rathi and improved by Shubham Bansal. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
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:
- Is it fine to write "void main()" or "main()" in C/C++?
- Difference between "int main()" and "int main(void)" in C/C++?
- C/C++ program for calling main() in main()
- Write a C program to print "Geeks for Geeks" without using a semicolon
- Write a C program to print "GfG" repeatedly without using loop, recursion and any control structure?
- return statement vs exit() in main()
- Functions that are executed before and after main() in C
- How to print "GeeksforGeeks" with empty main() in C, C++ and Java?
- How to change the output of printf() in main() ?
- Can main() be overloaded in C++?
- Executing main() in C/C++ - behind the scene
- What does main() return in C and C++?
- Write a one line C function to round floating point numbers
- Write a C macro PRINT(x) which prints x
- Write one line functions for strcat() and strcmp()
- When should we write our own copy constructor?
- Does C++ compiler create default constructor when we write our own?
- When should we write our own assignment operator in C++?
- Write a C program that does not terminate when Ctrl+C is pressed
- fopen() for an existing file in write mode
Improved By : ManasChhabra2


