Write a C/C++ program without using the main function
Write a C/C++ program without using the main() function. We are allowed to change the entry point of the program from main() to any other function or remove the main() function altogether.
1. Using GCC _start function
As per C/C++ standard, main() is the starting point of any program in a hosted environment where a program uses the facilities of an operating system. But in a freestanding environment, where a program execution may occur without the benefit of an operating system, the starting point needs not to be main(). An OS kernel or embedded system environment would be a good example of a freestanding environment.
It is worth pointing out that many other things happen before the main() function is executed, i.e., main() is not the first entry point of the program. If you’re using GCC, the _start function is the entry point of a C program that makes a call to main(). The main job of the _start function is to perform a few initialization tasks.
So, we can say that main() is the entry point for your program from a programmer’s perspective, and _start is the usual entry point from the Operating System perspective. We can override _start and ask the compiler for full control over what is happening right from the start using the -nostartfiles option.
|
1 2 3 4 5 6 7 8 9 10 |
// A program without using the main function // Compile it with gcc -nostartfiles #include <stdio.h> void _start() { printf("Inside _start\n"); _exit(0); } |
2. Using Static Initializer in C++
We can also use a static initializer in C++ to call any custom function before the main is executed. We can use the exit() function inside that custom function so that the program will terminate and control will never reach the main() function.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
// A program without using the main function #include <iostream> int execute() { std::cout << "Inside execute()"; exit(EXIT_SUCCESS); } static int s = execute(); int main() { std::cout << "Inside main() - never executed"; } |
Output:
Inside execute()
We can also make use of the C++ class constructor to do the same.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
// A program without using the main function #include <iostream> using namespace std; class Demo { public: Demo() // constructor { cout << "Inside Constructor " << endl; exit(EXIT_SUCCESS); } }; static Demo s; int main() { cout << "Inside main() - never executed"; } |
Output:
Inside Constructor
3. Using Macro Arguments
The ## macro operator concatenates two separate tokens together to form a single token. The following function uses the ## macro operator to hide the main method. However, the code still does make a call to the main() function behind the scenes, but just not in plain sight.
|
1 2 3 4 5 6 7 8 9 10 11 12 |
// A program without using the main function #include <stdio.h> #include <stdlib.h> #define replace(a,b,c,d) a##b##c##d #define execute replace(m,a,i,n) void execute() { printf("Hello World"); exit(0); } |
Output:
Hello World
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)