C Program Without a Main Function

C Program without a main functionHave you ever wondered how to write a C program without a main function? Can a C program execute with a main function? Is it possible to do that?

Well, the answer is YES! There can be a C program without a main function. Here is the source code of the program without a main function:

#define decode(s,t,u,m,p,e,d) m##s##u##t
#define begin decode(a,n,i,m,a,t,e)

int begin()
printf(” hello “);

The above program runs perfectly fine even without a main function. But how? What’s the logic behind it? How can we have a C program working without a main function. Read on to find out the answer…

Here, we are using a preprocessor directive called #define with arguments to give an impression that the program runs without the main function. However, in reality it runs with a hidden main function in it.

NOTE: A Preprocessor is program which processes the source code before compilation.

The ‘##‘ operator is called the token pasting or token merging operator. That is, we can merge two or more characters with it. Now, look at the 2nd line of program:

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

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

Now, look at the third line of the program:

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

Here the preprocessor replaces the macro “begin” 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 and the 2nd characters must be merged. In the argument (a,n,i,m,a,t,e) 4th, 1st, 3rd and the 2nd characters are ‘m’, ‘a’, ‘i’ and ‘n’.

So the third line “int begin” is replaced by “int main” by the preprocessor before the program is passed on to the compiler. That’s it.

The bottom line is that, there can never exist a C program without a main function. Here, we are just playing a gimmick that makes us believe that the program runs without the main, but there actually exists a hidden main function in the program. Here, we are using the proprocessor directive to intelligently replace the word “begin” by “main”. In simple words: int begin = int main.


  1. coolpyro
    March 3, 2008 at 8:11 AM

    gud one….

  2. happy
    March 3, 2008 at 4:04 PM


    keep it up!! gud work

  3. vishal dudhal
    March 12, 2008 at 3:39 PM

    hi friend i havent understand that how that begin will replace as main …

    and would u pls explain that second line once again

  4. Srikanth
    March 13, 2008 at 9:09 AM

    Lets see how the preprocessor replaces begin by main..

    When preprocessor scans the source code it encounters the word “begin” at the 4th line of the source code.So according to the macro definition in the third line the word “begin” is expanded into decode(a,n,i,m,a,t,e).
    But according to the macro definition of the second line decode(a,n,i,m,a,t,e) must be expanded into m##a##i##n (4th,1st,3d 2nd) characters

    Hence in the expanded source code(the source code after being processed by preprocessor and passed on to compiler) “begin” is eventually replaced by “main”…

    Now read the post as well as my comment and i hope you understand now…

  5. vishal dudhal
    March 13, 2008 at 12:34 PM

    ya got the idea clearly brother …

    have u any different program like this .. very tricky if u have then send on vishal.dudhal@gmail.com

  6. sampath
    June 28, 2008 at 7:48 PM

    hmmmmmmm amazing stuff…!! can u forward me some of d progrmes wid dat logic to sampath_kumar990@yahoo.com

  7. Anonymous
    September 6, 2008 at 7:47 AM

    hey i run this program but there is one error but dont read clearly and tell me some good programs ok!thanks 4 ur program !and i bless to u u became a good programmer

  8. suhas
    October 1, 2008 at 4:24 PM

    hai sri nice job… Programs are interesting… can u mail some interesting programs to me… here is my email sdev32@gmail.com

  9. Kameshwar
    January 25, 2009 at 11:45 PM

    very good one dude….. plz mail such prog to me… My id kameshwar.snayak@gmail.com

  10. Sanjay Jain
    February 17, 2009 at 6:01 PM

    good work buddy,can send me ur mail id i hav other prblem like same
    my id is snjy85@gmail.com

    • Srikanth
      February 17, 2009 at 11:36 PM

      @ Sanjay Jain

      Just fill up the Contact form with your problem and I’ll reply from my email ID.

  11. jayesh
    March 15, 2009 at 3:58 AM

    hey budy its nice…
    I want to know from where can I learn C programming deeply as you do??
    If you have some tuts or notes can you plz send me…
    also send me some egs of the above program

    • Srikanth
      March 15, 2009 at 10:29 AM

      @ jayesh

      You can learn more from Cprogramming.com

  12. Aditya
    March 16, 2009 at 7:54 PM

    hai sir

    here i had a doubt. if we use realloc() function in for loop
    which is infinite loop, will it occupy more memory in ram?

  13. kaustuv
    June 6, 2009 at 2:28 PM

    srikant bhai
    can u tell me hw to make a java into a link ?

  14. Abhishek
    June 6, 2009 at 5:40 PM

    Hello Srikant…
    its really a nice trick…thx for the such a cool stuuffffss.
    tk cr

  15. Anonymus
    June 25, 2009 at 7:41 PM

    dis is realy cool keep up the good work

  16. Anjaneai
    August 14, 2009 at 10:39 PM

    Awesome man … I knew Such Trick “Msut” be there but didnt knew .. u brought enlihtment gr8…… thanx ..

  17. vipan
    October 14, 2009 at 8:21 PM

    program without main()
    very interestingand tricky…

  18. Sandeep_S
    October 18, 2009 at 12:52 PM

    Thanks Srikant….
    superb…. superb….

  19. kishore
    November 9, 2009 at 10:58 PM

    Hello Shrikanth !
    myself Kishore sharma…
    may i have your Email Id please ?
    you are really good in programming !

  20. Dev
    November 10, 2009 at 10:23 PM

    can u post a bit few more short game codes?
    Dt wud b really interesting.

  21. naveen
    December 16, 2009 at 2:19 PM

    That was cool……

  22. GS Sekhon
    January 9, 2010 at 2:52 PM

    Its nice program

  23. priyankit
    January 13, 2010 at 6:17 PM


  24. Nishchint
    January 16, 2010 at 12:55 PM

    Good one !!
    It works

  25. jatin
    January 16, 2010 at 2:55 PM

    well done bro !!!!!!!
    keep it up bro

  26. satish
    February 1, 2010 at 1:17 PM

    Ok.Good logic.superb

  27. Vivek
    February 2, 2010 at 4:09 PM

    Nice code….
    Keep on dear..
    Good luck..

  28. tushar
    February 17, 2010 at 11:04 AM

    simply awesome , good work keep up , god bless U.

  29. shaiju
    February 19, 2010 at 7:46 PM

    Really amazing…….Publish more stuffs like this…Thanks..

  30. brijesh
    March 7, 2010 at 1:50 PM

    ha ha gud one ……

  31. Mitesh Shere
    April 1, 2010 at 6:26 PM

    Are mere Bhai Muze sirf ye bata
    ye sub tu shikha kahase.
    I want to be burn myself as yours.
    THANKS A LOT……… I can not explain.

  32. sivakumar
    April 14, 2010 at 5:38 PM

    Excellentttttttttttttt brother veryyyyyyyyyyyyyy goooooooooooooooooood keep it up.U have such gimmiks plz mail me my mail Id: kondal.sivak@gmail.com