Sunday, October 20, 2013

Program without using function main()

“ANSI C” Program without using main function


#include<stdio.h>
#include<conio.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()
{
     clrscr();
     printf("hello..friends");
     getch();
}


How..?
 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 processes the source code before compilation.
Look at the second 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 be expanded so that the 4th,1st,3rd & the
 Second characters must be merged .In the argument (a,n,i,m,a,t,e)
4th,1st,3rd & the second 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 we are using the processor directive to intelligently replace the word begin” by  “main” .

No comments: