C Program without a main() function
The main()
function is the starting point from where the program execution starts as per programmer's perspective. It usually has int
or void
return type.
It is followed by opening and closing curly braces which contain the program body. We can also pass parameters to it according to our requirement.
Can I execute a Program without main()
method?
Well, Yes, you can.
The program execution starts from the main()
function as per the programmer's perspective but it is not entirely true. Before the main()
function is executed, several other functions are called which prepare the environment variables for the program's execution, setup arguments, etc.
One such calls is made to the function _start()
. The _start()
function prepares the input arguments for another function _libc_start_main()
which then calls the main()
function. So, if we override the _start()
function, we can have any custom function from which our program will start execution. It does not have to be named main()
.
To do so, we will use -nostartfiles while compiling our program.
Below is a program without main()
. Save it in a file, say program.c file.
#include<stdio.h>
#include<stdlib.h>
int fun() // our custom main function
{
printf("Welcome to studytonight!\n");
return 0;
}
void _start()
{
int x = fun(); //calling custom main function
exit(x);
}
Compile it using this command,
gcc -nostartfiles -o program program.c
And then run it like,
./program
Welcome to studytonight!
Hiding the main() function in C Program
Below is a program without the main()
function.
In the below program, main()
function is there, but hidden using the preprocessors.
As you can see in the second line, #define decode()
function is used, which holds a character combination of m,a,i,n
and is followed by ##m##a##i##n
.
Here ##
operator is used to merge the characters in the order mentioned using ##
, which is main
In the 3rd line #define go decode(m,a,i,n)
as we have specified the characters in same order, the decode
function will assign value main
for go
.
#include<stdio.h>
//Need to include the following statements in same manner
#define decode(m,a,i,n) m##a##i##n
#define go decode(m,a,i,n)
int go()
{
printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");
printf("You have just executed your first program without making use of main() function!\n");
printf("\n\n\t\t\tCoding is Fun !\n\n\n");
return 0;
}
Program Output:
We can use different words and combination here, like
#define decode(s,t,u,m,p,e,d) m##s##u##t
#define go decode(a,n,i,m,a,t,e)
Here as per the first line, 4th, 1st, 3rd and 2nd chcracters have to be formed into a word. When the same combination is taken out from the word animate it makes main.
Using macro to define main
There is one more way to do this. Yes, using macro. Well in this technique, we will just create a facade, or in simpler words, we can say an illusion, where we are actually using main, but in the program, we use a different name for it.
#include<stdio.h>
#define go main
int go(void)
{
printf("Welcome to Studytonight");
return 0;
}
This is is the simplest technique, where all we have done is provided our main()
function with a different name, which is set the main
before the program is executed.
Using Token-Pasting operator
This is another way, again to just create an illusion. If someone else is reading this program, they will confused.
#include<stdio.h>
#define go m##a##i##n
int go(void)
{
printf("Welcome to Studytonight");
return 0;
}
Welcome to studytonight