Home » C SOURCE CODES

C Program Without a Main Function

Submitted by Srikanth on Monday, 3 March 200834 Comments

How to write a C program without a main function?. Is it possible to do that. Yes there can be a C program without a main function. Here’s the code of the program without a main function…
 

 
#include<stdio.h>
#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 “);
}

 

Does the above program run without the main function? Yes, the above program runs perfectly fine even without a main function. But how, whats the logic behind it? How can we have a C program working without main?

Here we are using preprocessor directive #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.

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

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 & 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 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 & 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 the third line “int begin” is replaced by “int main” by the preprocessor before the program is passed on for the compiler. That’s it…

The bottom line is there can never exist a C program without a main function. Here we are just playing a gimmick that makes us beleive the program runs without main function, but actually there 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.

Popularity: 2% [?]


  By using/following this site you agree to our Legal Disclaimer

Subscribe to GoHacking.Com


Enjoyed this article?
Subscribe to GoHacking.Com and get daily updates in your email for free

34 Comments »

  • coolpyro said:

    gud one….

  • happy said:

    superb!!!!!!!!!

    keep it up!! gud work

  • vishal dudhal said:

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

    and would u pls explain that second line once again

  • Srikanth (author) said:

    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…

  • vishal dudhal said:

    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

  • sampath said:

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

  • Anonymous said:

    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

  • suhas said:

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

  • Kameshwar said:

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

  • Sanjay Jain said:

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

  • Srikanth (author) said:

    @ Sanjay Jain

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

  • jayesh said:

    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 (author) said:

    @ jayesh

    You can learn more from Cprogramming.com

  • Aditya said:

    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?

  • kaustuv said:

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

  • Abhishek said:

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

  • Anonymus said:

    dis is realy cool keep up the good work

  • Anjaneai said:

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

  • vipan said:

    program without main()
    very interestingand tricky…

  • Sandeep_S said:

    Thanks Srikant….
    superb…. superb….

  • kishore said:

    Hello Shrikanth !
    myself Kishore sharma…
    may i have your Email Id please ?
    you are really good in programming !
    :)
    kishore.always1@gmail.com

  • Dev said:

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

  • naveen said:

    That was cool……

  • GS Sekhon said:

    Hi
    Its nice program

  • priyankit said:

    HEY CLEVER BOY GOOD CREATIVITY well done

  • Nishchint said:

    Good one !!
    It works

  • jatin said:

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

  • satish said:

    Ok.Good logic.superb

  • Vivek said:

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

  • tushar said:

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

  • shaiju said:

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

  • brijesh said:

    ha ha gud one ……

  • Mitesh Shere said:

    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.

  • sivakumar said:

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