C Program without main function

Here’s the solution:
#include
 
#define decode(i,s,r,a,e,l) s##i##r##a
 
#define begin decode(a,m,i,n,o,l)
 
void begin()
 
{
 
printf(“No Main Function !!!);
 
}



Does the above program run without main function? Yes, the above program runs perfectly fine even without a main function. Now, your question is “HOW? What’s the logic behind in it? How can we have a C program working without main()?”.

Now, we’ll see how. Here we are using preprocessor directive #define with arguments to give an impression that the program runs without main. But in realty it runs with a hidden main function. The ‘##’ operator is called the token passing or token merging operator (i.e., we can merge two or more characters with it). I’m assuming that you already know the preprocessor is a program which processes the source code before compilation.

Look at the second line of the program. #define decode(i,s,r,a,e,l) s##i##r##a

What the preprocessor doing here? The macro decode is being expanded as “sirn” (The ## operator merges s,i,r,a into sira). The logic is when you pass (s,o,l,o,m,o,n) as argument, it merges the 2nd, 1st, 3rd and 4th characters.

Now look at the third line of the program #define begin decode(a,m,i,n,o,l)



Here the preprocessor replaces the macro “begin” with expansion of decode(a,m,i,n,o,l). According to the macro definition in the previous line the argument must be expanded, so that the 2nd, 1st, 3rd & 4th characters must be merged. In the argument (a,m,i,n,o,l) 2nd, 1st, 3rd & 4th characters are ‘m’,’a’,’i’,’n’.

So that the third line “void begin” is replaced by “void main” by the preprocessor before the program is passed on for the compiler. 

Happy Learning

Leave a Reply

Your email address will not be published. Required fields are marked *