## No main's land

Monday, August 20, 2012

The following code compiles correctly with a standard C compiler.

#include <stdio.h>

#define set(b,r,e,a,k,p,o,i,n,t,s) n##s##i e##o##n##s
#define begin set(c,o,m,p,u,t,a,t,i,o,n)

begin()
{
printf("hello, world\n");
return 0;
}


Not only does it compile properly, it links into an executable that executes and prints the string "hello, world" followed by a newline. How does this code compile, link and execute successfully even though it doesn't appear to have a main function?

Tags:
|« « # » »|

### 1 comment

#### Himanshu Tarksheel solved this quiz:

The begin macro is replaced with set(c,o,m,p,u,t,a,t,i,o,n). This is replaced with i##n##t m##a##i##n because the parameters n, s, i, e, o, n, s in the parameter list of the definition of set macro corresponds to the actual parameters i, n, t, m, a, i, n in set(c,o,m,p,u,t,a,t,i,o,n). The ## operator concatenates multiple tokens into one token. Therefore, begin() is replaced with int main().

|« « # » »|

### Post a solution or comment

You may also email your solutions directly to quiz@fabhax.com.
Only correct and neat solutions will be published.