Have you ever wondered how to write a C program without a main function? Can a C program execute with a main function? Is it possible to do that?
Well, the answer is YES! There can be a C program without a main function. Here is the source code of the program without a main function:
#include<stdio.h>
#define decode(s,t,u,m,p,e,d) m##s##u##t
#define begin decode(a,n,i,m,a,t,e)
int begin()
{
printf(" hello ");
}
The above program runs perfectly fine even without a main function. But how? What’s the logic behind it? How can we have a C program working without a main function. Read on to find out the answer…
Here, we are using a preprocessor directive called #define with arguments to give an impression that the program runs without the main function. However, in reality it runs with a hidden main function in it.
The ‘##‘ operator is called the token pasting or token merging operator. That is, we can merge two or more characters with it. Now, look at the 2nd line of program:
What is the preprocessor doing here? The macro decode(s,t,u,m,p,e,d) is being expanded as “msut” (The ## operator merges m, s, u and t into msut). The logic is, when you pass (s,t,u,m,p,e,d) as argument it merges the 4th, 1st, 3rd and the 2nd characters (tokens).
Now, look at the third line of the program:
Here the preprocessor replaces the macro “begin” with the expansion decode(a,n,i,m,a,t,e). According to the macro definition in the previous line, the argument must be expanded so that the 4th, 1st, 3rd and the 2nd characters must be merged. In the argument (a,n,i,m,a,t,e) 4th, 1st, 3rd and the 2nd characters are ‘m’, ‘a’, ‘i’ and ‘n’.
So the third line “int begin” is replaced by “int main” by the preprocessor before the program is passed on to the compiler. That’s it.
The bottom line is that, there can never exist a C program without a main function. Here, we are just playing a gimmick that makes us believe that the program runs without the main, but there actually exists a hidden main function in the program. Here, we are using the proprocessor directive to intelligently replace the word “begin” by “main”. In simple words: int begin = int main.


Hide Data in Image, Audio and Video Files: Steganography
Ways to Encrypt Data to Store in Cloud
How to Compile C Programs
How Windows Product Activation (WPA) Works?
gud one….
superb!!!!!!!!!
keep it up!! gud work
hi friend i havent understand that how that begin will replace as main …
and would u pls explain that second line once again
Lets see how the preprocessor replaces begin by main..
When preprocessor scans the source code it encounters the word “begin” at the 4th line of the source code.So according to the macro definition in the third line the word “begin” is expanded into decode(a,n,i,m,a,t,e).
But according to the macro definition of the second line decode(a,n,i,m,a,t,e) must be expanded into m##a##i##n (4th,1st,3d 2nd) characters
Hence in the expanded source code(the source code after being processed by preprocessor and passed on to compiler) “begin” is eventually replaced by “main”…
Now read the post as well as my comment and i hope you understand now…
ya got the idea clearly brother …
have u any different program like this .. very tricky if u have then send on [email protected]
hey i run this program but there is one error but dont read clearly and tell me some good programs ok!thanks 4 ur program !and i bless to u u became a good programmer
hai sri nice job… Programs are interesting… can u mail some interesting programs to me… here is my email [email protected]
Kameshwar says
very good one dude….. plz mail such prog to me… My id [email protected]
Sanjay Jain says
good work buddy,can send me ur mail id i hav other prblem like same
my id is [email protected]
@ Sanjay Jain
Just fill up the Contact form with your problem and I’ll reply from my email ID.
jayesh says
hey budy its nice…
I want to know from where can I learn C programming deeply as you do??
If you have some tuts or notes can you plz send me…
also send me some egs of the above program
@ jayesh
You can learn more from Cprogramming.com
hai sir
here i had a doubt. if we use realloc() function in for loop
which is infinite loop, will it occupy more memory in ram?
kaustuv says
srikant bhai
can u tell me hw to make a java into a link ?
Abhishek says
Hello Srikant…
its really a nice trick…thx for the such a cool stuuffffss.
tk cr
Anonymus says
dis is realy cool keep up the good work
Anjaneai says
Awesome man … I knew Such Trick “Msut” be there but didnt knew .. u brought enlihtment gr8…… thanx ..
program without main()
very interestingand tricky…
Thanks Srikant….
superb…. superb….
kishore says
Hello Shrikanth !
myself Kishore sharma…
may i have your Email Id please ?
you are really good in programming !
🙂
[email protected]
Dev says
can u post a bit few more short game codes?
Dt wud b really interesting.
naveen says
That was cool……
GS Sekhon says
Hi
Its nice program
priyankit says
HEY CLEVER BOY GOOD CREATIVITY well done
Nishchint says
Good one !!
It works
jatin says
well done bro !!!!!!!
keep it up bro
satish says
Ok.Good logic.superb
Vivek says
Nice code….
Keep on dear..
Good luck..
tushar says
simply awesome , good work keep up , god bless U.
shaiju says
Really amazing…….Publish more stuffs like this…Thanks..
brijesh says
ha ha gud one ……
Mitesh Shere says
Are mere Bhai Muze sirf ye bata
ye sub tu shikha kahase.
I want to be burn myself as yours.
THANKS A LOT……… I can not explain.
sivakumar says
Excellentttttttttttttt brother veryyyyyyyyyyyyyy goooooooooooooooooood keep it up.U have such gimmiks plz mail me my mail Id: [email protected]