C Program Without Main Function
Main function call the other functions to perform there tasks. As there are Managers in Hotels. Main is the Manager of C programming.
Now a Logical Question arises.
Q.Can we Build a C Program Without Main Function?
Ans. Yes , It is possible to write a C Program without defining a Main function.Just type the following code in your C editor. (For example Turbo C/C++):
- #include
- #include
- #define decode(s,t,u,m,p,e,d) m##s##u##t
- #define kgtricks decode(a,n,i,m,a,t,e)
- int kgtricks()
- {
- clrscr();
- printf(" C Program Without Main function");
- getch();
- return 0;
- }
- OUTPUT:
- C Program Without Main function
Now most of you still may be wondering about the fact that how come the above program run without Main Function.
The secret is that the preprocessor directory #define line of code combined with decode work together to form a hidden Main function.
The '##' operator is known as the token pasting or token merging operator. We can merge two or more characters using it.
Preprocessor : A program which process source code before compilation.
In the Line '#define decode(s,t,u,m,p,e,d)m##s##u##t
The macro decode(s,t,u,m,p,e,d) is expanded as "msut".
The ## operator merger m,s,u & t into msut.
Now look at "#define kgtricks decode(a,n,i,m,a,t,e)
Here the macro kgtricks is been replaced with expansion decode (a,n,i,m,a,t,e)
According to the macro definition in the lines programmed above, the argument must be expanded so that the 4th, 1st , 3rd , and the 2nd characters must be merged . In this case (a,n,i,m,a,t,e) , the arguments are 4th, 1st , 3rd and 2nd character. The term will show up as m,a,i,n
Here in the third line
int kgtricks is replaced by int main by the preprocessor .
Hence it can be said that there can not be any program without a main function. This trick in the above program just hides the main function from the user. Use of preprocessor intelligently replaces the word "kgtricks" with "main"
In other words int kgtricks= int main
Also Read:
Hey admin,
ReplyDeleteof course this blog is doing a very good job of serving useful information. I'm proud to be a part of its Readers community.good job dude...
for more c programing visit my web http://www.hhhprogram.com/
Thanks for the compliments Hardik. We are glad to have readers like you :)
Delete