How to write a C program without "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
This can be achieved by using two methods and are as follows
- Indirect Method
- Direct Method
Indirect Method:
In this indirect method, we will use the main function indirectly and that is with the help of macro's. Please take a look at the code snippet given below
Example1:
#include <stdio.h>
#define fun main
int fun( void )
{
printf ( "This is My Main Function" );
return 0;
}
|
From the above code, fun is the function which i am using instead of main and fun is a macro which equivalent is "main". While compiling the above program, fun gets replaced by main at the time of preprocessing stage. After preprocessing stage code almost looks same like we used main.
Example 2:
#include <stdio.h>
#define fun m##a##i##n
int
fun(
void
)
{
printf
(
"This is My Main Function"
);
return
0;
}
The example 2 is almost same as example 1. The only difference is that, example 2 uses tokens in macro, which will concatenate the m,a,i,n and forms a string called "main".
Direct Method:
In this method, we will directly use custom main function and there is no main concept at all.
Always compiler uses _start as the starting point for any program. The _start function always looks for an entry point from the user program and that entry point is fixed and named as "main". So whenever we use start files (_start) which always looks for main and that's why main is necessary.
In order to avoid the main function from our program, we shouldn't use the start files (_start). While compiling our program we should tell to the compiler that, "No need to use start files". We can intimate to the compiler that not to use start files by using compiler arguments and is given below.
gcc test.c -nostartfiles -o test_bin
Example 3:
#include <stdio.h>
int
fun(
void
)
{
printf
(
"This is My Function"
);
exit(0)
; //return statement shouldn't use, cause segmentation fault
}
If you compile the above program (example 3) with "-nostartfiles" option, then binary will gets created.
From the above program (example 3) we should note few important things and are listed below.
1. We shouldn't use return statement, why because as we haven't used _start, So no one will be ready to collect our return status. If we try to use return statement, which will leads to segmentation fault.
2. exit(0) statement tells the loader to unload the program from the memory. Why because loader is the one who loads our program into memory.
What if multiple functions definitions present?
If multiple functions (definitions) present in the file, then the compiler will choose one entry point for the program. And the compiler always choose first function which defined in the program.
Example 4:
#include <stdio.h>
int
fun(
void
)
{
printf
(
"This is My fun Function"
);
exit(0)
; //return statement shouldn't use, cause segmentation fault
}
int print(void
)
{
printf
(
"This is My print Function"
);
exit(0)
; //return statement shouldn't use, cause segmentation fault
}
If we compile and run the above program (example 4), then we will get output as "This is My fun Function".
If we want to select our function as entry point, then we should intimate to the compiler while compiling program by using option.
From the example if i want to use print as my entry point, then i should compile by specifying entry point option.
gcc test.c -nostartfiles -e print -o test_bin
"-e" option tells the compiler as an entry point.
Case Study:
We can use customized entry point option even when the main present. See the code below.
Example 5:
#include <stdio.h>
int
main(
void
)
{
printf
(
"This is My main Function"
);
exit(0)
; //return statement also works
}
int print(void
)
{
printf
(
"This is My print Function"
);
exit(0)
; //return statement shouldn't use, cause segmentation fault
}
From the above example we can compile and run the program by using entry point option.
gcc test.c -e print -o test_bin
If we runt the test_bin binary, then the output will be "This is My print Function".
***************************** Thanks for Reading ***************************