Wednesday 20 February 2013

Can you write a "C" program with out main() ....???

/*
The answer is YES. Try the following code
*/
#include<stdio.h>
#define decodes(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");
}

// This is possible in Turbo C. In linux we've anothe way.


Here is how it works once we say define we need to understand that
#define x y
then ‘x’ ix replaced by ‘y’
similarly in this case
#define begin decode(a,n,i,m,a,t,e)
decode(a,n,i,m,a,t,e) is replaced by m##a##i##n
bcoz s is replaced by a,t by n,u by i and so on
s->a
t->n
u->i
m->m
p->a
e->t
d->e
now the statement becomes
void m##a##i##n
And u must be knowing that ## is used for string concatenation so it becomes
“main”
finally the code crops down to
void main()
{
printf(“hello”);
}

No comments:

Post a Comment