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.
Popularity: 2% [?]
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 vishal.dudhal@gmail.com
hmmmmmmm amazing stuff…!! can u forward me some of d progrmes wid dat logic to sampath_kumar990@yahoo.com
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 sdev32@gmail.com
very good one dude….. plz mail such prog to me… My id kameshwar.snayak@gmail.com
good work buddy,can send me ur mail id i hav other prblem like same
my id is snjy85@gmail.com
@ 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 !
kishore.always1@gmail.com
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: kondal.sivak@gmail.com
EMAIL SECURITY »
What to do when your Email Account is Hacked?
How to Recover Hacked Email Accounts?
It can be a real nightmare if someone hacks and takes control of your email account as it may contain confidential information like bank logins, credit card details and other sensitive data. …
HOW-TO GUIDES »
How to Detect Anonymous IP Addresses
As the fraudsters are now becoming more sophisticated in bypassing the Geo-location controls by using proxies (Anonymous IPs) to spoof their IP address, it has become very much necessary to come up with a means …
INTERNET SECURITY »
How to Detect Anonymous IP Addresses
As the fraudsters are now becoming more sophisticated in bypassing the Geo-location controls by using proxies (Anonymous IPs) to spoof their IP address, it has become very much necessary to come up with a means …
NETWORK HACKS »
How to Hack an Ethernet ADSL Router
Almost half of the Internet users across the globe use ADSL routers/modems to connect to the Internet however, most of them are unaware of the fact that it has a serious vulnerability which can easily be exploited even by a …
VIRUS CREATION »
How to Test the Working of your Antivirus – EICAR Test
Have you ever wondered how to test your Antivirus software to ensure it’s proper working? Well here is a quick and easy way to test your antivirus. The process is called EICAR test which will …
Categories
Blogroll
Recent Comments
Most Commented
Most Popular
Log In | Entries (RSS) | Comments (RSS) | Sitemap