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: c
Newer | Older