C Program Without a Main Function
How to write a C program without a main function?. Is it possible to do that. Yes there can be a C program without a main function. Here’s the code of the program without a main function…
#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 “);
}
Does the above program run without the main function? Yes, the above program runs perfectly fine even without a main function. But how, whats the logic behind it? How can we have a C program working without main?
Here we are using preprocessor directive #define with arguments to give an impression that the program runs without main. But in reality it runs with a hidden main function.
The ‘##‘ operator is called the token pasting or token merging operator. That is we can merge two or more characters with it.
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 & t into msut). The logic is when you pass (s,t,u,m,p,e,d) as argument it merges the 4th,1st,3rd & 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 & the 2nd characters must be merged. In the argument (a,n,i,m,a,t,e) 4th,1st,3rd & the 2nd characters are ‘m’,'a’,'i’ & ‘n’.
So the third line “int begin” is replaced by “int main” by the preprocessor before the program is passed on for the compiler. That’s it…
The bottom line is there can never exist a C program without a main function. Here we are just playing a gimmick that makes us beleive the program runs without main function, but actually there 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.
By using/following this site you agree to our Legal Disclaimer

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]
hmmmmmmm amazing stuff…!! can u forward me some of d progrmes wid dat logic to [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]
very good one dude….. plz mail such prog to me… My id [email protected]
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.
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?
srikant bhai
can u tell me hw to make a java into a link ?
Hello Srikant…
its really a nice trick…thx for the such a cool stuuffffss.
tk cr
dis is realy cool keep up the good work
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….
Hello Shrikanth !

myself Kishore sharma…
may i have your Email Id please ?
you are really good in programming !
[email protected]
can u post a bit few more short game codes?
Dt wud b really interesting.
That was cool……
Hi
Its nice program
HEY CLEVER BOY GOOD CREATIVITY well done
Good one !!
It works
well done bro !!!!!!!
keep it up bro
Ok.Good logic.superb
Nice code….
Keep on dear..
Good luck..
simply awesome , good work keep up , god bless U.
Really amazing…….Publish more stuffs like this…Thanks..
ha ha gud one ……
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.
Excellentttttttttttttt brother veryyyyyyyyyyyyyy goooooooooooooooooood keep it up.U have such gimmiks plz mail me my mail Id: [email protected]
EMAIL SECURITY »
How Do Email Spam Filters Work
If you are the one who works with emails on a daily basis, you are most likely to be using a SPAM FILTER to ease the job of sifting through a large number of spam emails …
HOW STUFFS WORK »
How Antivirus Software Works
Due to ever increasing threat from virus and other malicious programs, almost every computer today comes with a pre-installed antivirus software on it. In fact, an antivirus has become one of the most essential software package for every computer.
Even though every one …
HOW-TO GUIDES »
How to Retrieve Clipboard History in Windows
Ever copied a code snippet or text from the web and forgot to paste it before you copied something else? Well, all of us will have an experience of something like this, where we want to …
INTERNET SECURITY »
Access Your Facebook Account with 3 Passwords
Did you know that you can login to your Facebook account using 3 different passwords? Seems interesting isn’t it? Yep! Unlike any other online account which has only one password to access, Facebook lets you …
NETWORK HACKS »
Access Your Facebook Account with 3 Passwords
Did you know that you can login to your Facebook account using 3 different passwords? Seems interesting isn’t it? Yep! Unlike any other online account which has only one password to access, Facebook lets you …
Categories
I am the author and founder of GoHacking.Com. It all started in November 2007 as a small blog with just a few pages.
However, with the passage of time, the blog picked up more traffic and the response from the readers was also increased. This made me take up blogging more seriously.
You can read more about me or get in touch with me on Google+ or contact page.
Most Commented
Blogroll