Wednesday 13 June 2012

C program without main keyword


Hello, 
                   Save the following program as *.c and run it.


//simple c program:-


#include<stdio.h>
#define usethis(h,c,e,t,o,p,q)t##e##c##h
#define begin usethis(n,i,a,m,a,t,p)
int begin()
{
printf("Program without main keyword...\n");
}

                    
Are u able to see the output.

Reason:-
1]## operators acts as token merging operator..we can merge two or more character in it.

2]#define usethis(h,c,e,t,o,p,q)t##e##c##h

The macro usethis(h,c,e,t,o,p,q) is being expanded as "tech" (the ## operator merges t,e,c & h into tech).
The logic when you pass (h,c,e,t,o,p,q) as argument it merges the 4th,3rd,2nd & 1st chacter

3]Now see the #define begin usethis(n,i,a,m,a,t,p)

The preprocessor macro begin replaces with expansion of the usethis(n,i,a,m,a,t,p)
The argument must be expanded so that that  4th,3rd,2nd & 1st character must be merged.

In the argument (n,i,a,m,a,t,p)  4th,3rd,2nd & 1st character are 'm','a','i','n'
thats it.

1 comment:

Cool Blue Outer Glow Pointer