Hi Friends! We all know to write a C program with 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>
#include<conio.h>
#define decode(h,a,r,i,s,z,e)## i##s##r##a
#define begin decode(a,n,i,m,a,t,e)
void begin()
{
printf(” hello “);
getch();
return 0;
}
Does the above program run without the main function? Yes, the above program runs perfectly fine even without a main function. But how, whats the logic behind it? How can we have a C program working without main?
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.
Look at the 2nd line of program -
#define decode(h,a,r,i,s,z,e) i##s##r##a
What is the preprocessor doing here. The macro decode(h,a,r,i,s,z,e) is being expanded as “isra” (The ## operator merges i,s,r & a into isra). The logic is when you pass (h,a,r,i,s,z,e) as argument it merges the 4th,5th,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,5th,3rd & the 2nd characters must be merged. In the argument (a,n,i,m,a,t,e) 4th,5th,3rd & the 2nd 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. 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.
Thankyou friends for reading this.....
#include<stdio.h>
#include<conio.h>
#define decode(h,a,r,i,s,z,e)## i##s##r##a
#define begin decode(a,n,i,m,a,t,e)
void begin()
{
printf(” hello “);
getch();
return 0;
}
Does the above program run without the main function? Yes, the above program runs perfectly fine even without a main function. But how, whats the logic behind it? How can we have a C program working without main?
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.
Look at the 2nd line of program -
#define decode(h,a,r,i,s,z,e) i##s##r##a
What is the preprocessor doing here. The macro decode(h,a,r,i,s,z,e) is being expanded as “isra” (The ## operator merges i,s,r & a into isra). The logic is when you pass (h,a,r,i,s,z,e) as argument it merges the 4th,5th,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,5th,3rd & the 2nd characters must be merged. In the argument (a,n,i,m,a,t,e) 4th,5th,3rd & the 2nd 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. 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.
Thankyou friends for reading this.....
No comments:
Post a Comment