/* Ajith - Syntax Higlighter - End ----------------------------------------------- */

8.23.2012

Write a C Program without main function

In a C program main function is the entry point so it is mandatory to have a main function.

But let us see how can we write a program without main (Kind of hiding main in some obfuscated code). This post is purely out of interest to know that we can do something weird like this, just for learning.


Solution 01: Using simple macro substitution
#include <stdio.h>

#define START main

int START()
{
   printf("HELLO WORLD");
}
During the preprocessing stage of compilation of above code, HELLO is replaced with main.

Solution 02: Using Token Merging Operator

## is a Token merging operator. It can merge two or more characters.
#include <stdio.h>

#define START m##a##i##n

int START()
{
   printf("HELLO WORLD");
}
Solution 03: Using Argumented Macro
#include <stdio.h>

#define decode(l,i,g,h,t) h##l##g##i
#define START decode(a,n,i,m,e)

int START()
{
   printf("HELLO WORLD");
}
During the preprocessing stage START is replaced with decode which takes 5 arguments namely a n i m e and then decode is replaced h##l##g##i which means main.

No comments :

Post a Comment

Your comments are moderated