Pages

Monday, 21 September 2015

Program Without Main

Want to know C program without main
Want to know C program without main

When I got this problem, I thought that this is a weird idea to write a C code without main. But when I study a few books and articles I thought that it’s a very interesting topic, so I am trying  to share with you,that how this thing is happening.Actually, it is just an eyewash to write a code without main.

To actually do this thing, you can use a very simple concept which is known as “macro”. I think all of you have the knowledge of the pros and cons of the macro. But still I am giving a brief overview about this, as it’s takes a part of my discussion.

“Macro is a token which is used to replace a symbolic constant”

For example
#define PI 3.14285

So in this program any symbolic constant ‘PI’ is replaced by the ‘3.14285' token.
So it is advantageous if your program uses the PI value several times. You have to just change the  macro definition and it reflects the same value or token  anywhere you write PI in the  program. Now we are using  the concept of Macro to define a C program without main.

#include<stdio.h>
#define begin main
int begin()
{
            printf(“Program without main”);
            return 0;
}

Now a question arises that whether it will work or not? But  I can confirm you it will work without any problem.
Now our point of discussion is that what is the keen idea behind the seen? Actually the idea is all about “Macro” which makes this scheme weird but interesting.

Here we are defineing begin as a symbolic constant , so in this code, anywhere you write ‘begin’ it is replaceing with it’s token ‘main’. So the code fragment ‘int begin()’ in run time treated as int main() due to the replacement of ‘begin’ with ‘main’. So your program runs smoothly.

Now we are trying to complicate this idea by adding another very interesting concept known as “token marging” . Before I give you the complication pain,it is my responsibility to provide you the “token marging” concept.
Now consider code segment and its output,

#include<stdio.h>
#define merge(a,b) a##b
int main()
{
            int merge(a,b)=12;
            printf("Value in ab=%d",ab);
            return 0;
}
You will get it’s output as ‘Value in ab=12’ , where 12 is the value of the variable named as ‘ab’. But there is no variable in this program which is defined as ‘ab’. So what is the actually happening behind the scene? .’##’ in C have some special meaning,which is known as ‘token pasting’ operator. Here I am using this concept as wild manner. Here merge(a,b) is replaced by ‘ab’ because when we are using macro function with various arguments, then we can merge the arguments with the help of ‘##’.
So  if you define any macro function as
#define func_macro_name(A1,A2,…,An) A1##A2##...##An
it means that macro function is replacing  by A1A2…An. Thus in the last case we can see that  ‘merge(a,b)’ is replaced by ‘ab’ and here we are defineing an integer variable using a different way.  I think I am able to penetrate this idea in your mind , so I am again rewriteing this code without main.

#include<stdio.h>
#define begin m##a##i##n
int begin()
{
            printf("Program without main");
            return 0;
}

So here symbol ‘begin’ is replaced by ‘main’. So again your program runs smoothly. Now I am extending my weird concept a little bit. So here is my another brain storming code without main.

#include<stdio.h>
#define decode(r,a,p,i,d,l,y) r##a##i##n
#define begin decode(m,a,g,i,c,a,l)
int begin()
{
 printf("!!C program without main");
 return 0;
}

Now we are trying to explain this concept step by step.
At first ‘begin’ is replacing by decode(m,a,g,i,c,a,l) . But decode(m,a,g,i,c,a,l) defined as a ‘function like macro’. So here we are passing the value to the actual macro function.
Where  ‘r’ get ‘m’, ‘a’ get ‘a’, ‘p’ get ‘g’, ‘i’’ get ‘i’, ‘d’ get ‘’c’, ‘l’ get ‘a’ and ‘y’ get ‘l’. Now if any one write r##a##i##n, then it means we are mergeing the constant define by each symbol.
So ‘r’ means ‘m’, ‘a’’ means ‘a’, ‘i’ means ‘i’ merge with ‘n’ written as constant.
Thus we have actually formed main using a different way. So I can conclude that in C without main() function you can’t write any things, but if you wish you can make some trick to confuse the other people. Because sometime confusion leads a new way.