Last Updated : 23 Jul, 2025
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:-
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:-
#include<stdio.h>
#define fun main
int fun(void)
{
printf("Geeksforgeeks");
return 0;
}
Output: Geeksforgeeks
2. 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)
C
#include<stdio.h>
#define fun m##a##i##n
int fun()
{
printf("Geeksforgeeks");
return 0;
}
Output: Geeksforgeeks
3. Using Argumented Macro
C
#include<stdio.h>
#define begin(m,a,i,n) m##a##i##n
#define start begin(m,a,i,n)
void start() {
printf("Geeksforgeeks");
}
Output: Geeksforgeeks
4. Modify the entry point during compilation
C
#include<stdio.h>
#include<stdlib.h>
// entry point function
int nomain();
void _start(){
// calling entry point
nomain();
exit(0);
}
int nomain()
{
puts("Geeksforgeeks");
return 0;
}
Output: GeeksforgeeksCompilation 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()
Refer Executing main() in C – behind the scene for another solution. References: Macros and Preprocessors in C
Time Complexity: O(1)
Auxiliary Space: O(1)
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4