16 October 2011

C program without main function

Hi friends hope you are all fine. 
All of you have must read my previous article on How to print something without using a semicolon in C. This time i have come up with some thing more crazier. What about a program in C without using a main function  

Is it possible to do that ? 
The ans is simply no.

C does't allow us to write a program without using a main function.

Here is the source code

#include <stdio.h>

#define decode(s,t,u,m,p,e,d) m##s##u##t

#define ravish decode(a,n,i,m,a,t,e)

void ravish()

{

printf("stupid, we can do anything ");

}



This program will work perfectly without main function. But the million dollar question is whats the logic behind this . As i said earlier no C program will run without a main function. In above case we have just tricked the compiler to assume that ravish = main .

Here we are using preprocessor directive( A Preprocessor is program which processess the source code before compilation ) #define with arguments to give an impression that the program runs without main. But in reality it runs with a hidden main function.


The ‘##‘ operator is called the token pasting or token merging operator. That is we can merge two or more characters with it.

Look at the 2nd line of code :

#define decode(s,t,u,m,p,e,d) m##s##u##t

The macro decode(s,t,u,m,p,e,d) is being expanded as “msut” (The ## operator merges m,s,u & t into msut). The logic is when you pass (s,t,u,m,p,e,d) as argument it merges the 4th,1st,3rd & the 2nd characters(tokens).

Now look at the third line of code :

#define ravish decode(a,n,i,m,a,t,e)

Here the preprosser replaces the macros "ravish" with the expansion decode(a,n,i,m,a,t,e) .
According to the macro definition in the previous line the argument must be expanded so that the 4th,1st,3rd & the 2nd characters must be merged. In the argument (a,n,i,m,a,t,e) 4th,1st,3rd & the 2nd characters are ‘m’,'a’,'i’ & ‘n’.

So now the next line of code void ravish is replaced by void main before it can be passed to the compiler.

Moral of this article is there never exist a program in c without a main function here we are just playing a gimmick that make people believe that the program runs without a main function .

2 comments:

  1. This professional hacker is absolutely reliable and I strongly recommend him for any type of hack you require. I know this because I have hired him severally for various hacks and he has never disappointed me nor any of my friends who have hired him too, he can help you with any of the following hacks:

    -Phone hacks (remotely)
    -Credit repair
    -Bitcoin recovery (any cryptocurrency)
    -Make money from home (USA only)
    -Social media hacks
    -Website hacks
    -Erase criminal records (USA & Canada only)
    -Grade change

    Email: onlineghosthacker247@ gmail .com

    ReplyDelete
  2. Do you need to increase your credit score?
    Do you intend to upgrade your school grade?
    Do you want to hack your cheating spouse Email, whatsapp, Facebook, instagram or any social network?
    Do you need any information concerning any database.
    Do you need to retrieve deleted files?
    Do you need to clear your criminal records or DMV?
    Do you want to remove any site or link from any blog?
    you should contact this hacker, he is reliable and good at the hack jobs..
    contact : cybergoldenhacker at gmail dot com

    ReplyDelete