Wednesday 27 February 2013

C/C++ Puzzles: PART - 22

Obfuscating the Main( )

  Consider the C Program given below. The main( ) function is not immediately visible. But we know that every C Program requires a main( ). If you carefully analyze the program you will understand the working of it.

#include <stdio.h>

#define _(__,___,____,_____,______,_______) _____##__##____##___

#define NO_MAIN _(a,n,i,m,a,l)

NO_MAIN()
{
    printf("\nSurprise!!!\n");
}

Figure Below Shows the Output

Click here to download the C Program.

Explanation:
  The program uses preprocessor directives to hide the main( ). The preprocessor replaces NO_MAIN with the parameterized macro argument named single underscore ( _ ). Remember single underscore is a valid identifier. This macro replaces the letters a,n,i,m,a,l with main using Token Pasting Operator (##). Thus NO_MAIN is replaced with macro argument named single underscore which in turn is replaced with the word main. Also notice that the identifiers inside the macro too are made up of underscore. That made the program even more confusing.

Figure Below Shows the Substitutions Made by the Preprocessor
   

No comments:

Post a Comment