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...
#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 ");
}
Yes, the above program runs perfectly fine even without a main function.But how,whats the logic behind it?
Here we are using preprocessor directive #define with arguments.The '##' operator is called the token pasting or token merging operator.That is we can merge two or more characters with it.
NOTE: A Preprocessor is program which processess the source code before compilation.
Look at the 2nd line of program-
#define decode(s,t,u,m,p,e,d) m##s##u##t
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-
#define begin decode(a,n,i,m,a,t,e)
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 de 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 here we are using the proprocessor directive to intelligently replace the word begin" by "main" .In simple words int begin()=int main().
Is you are still not able to understand the code plz pass comments.I'll try to clear your thoughts..
Readers Who Read This Page, Also Read:
A Self Destruction Program In C
A Guessing Game In C
A C Program To Generate Random Numbers
Pigeon Breeding Problem Solved
What is Ransomware and How to Stay Protected
9 years ago
8 comments:
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
Post a Comment