Thursday, April 22, 2010

c-program without main function

C PROGRAM RUNS WITHOUT MAIN FUNCTION ( BELIVE IT OR NOT)


#include

#define decode(i,m,r,a,n) a##i##r##m

#define imran decode(a,n,i,m,a)

int imran()
{
printf(" hello imme ");
}







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.

What is the preprocessor doing here.The macro decode(i,m,r,a,n) is
being expanded as "airm" (The ## operator merges a,i,r & m into
airm).The logic is when you pass (i,m,r,a,n) as argument (tokens).it
merges the 4th,1st,3rd & the 2nd characters


The logic is when you pass (a,n,i,m,a) as argument it merges
4th,1st,3rd, 2nd characters then it becomes main()


Now look at the third line of the program-

#define begin decode(a,n,i,m,a)


Here the preprocessor replaces the macro "imran" with the expansion
decode(a,n,i,m,a).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) 4th,1st,3rd &
the 2nd characters are 'm','a','i' & 'n'.

So the third line "int imran()" is replaced by "int main()" by the
preprocessor before the program is passed on for the compiler.


That's it...


THIS LOGIC DEVELOPED BY IMRAN KHAN(imme)
6th SEM(COMPUTER SCIENCE AND ENGINEERING),
RAAJDHANI ENGINEERING COLLEGE,MANCHESWAR,BHUBANESWAR,INDIA

No comments:

Post a Comment