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…#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 “);
}
#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 “);
}
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.
NOTE: A Preprocessor is program which processess the source code before compilation.
#define decode(s,t,u,m,p,e,d) m##s##u##t
#define begin decode(a,n,i,m,a,t,e)
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.
No comments:
Post a Comment