C Program without main function

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.

13 Responses to “C Program without main function”

  1. at home game tester

    Jan 27. 2011

    I was looking for this. Thanks a lot.

    Reply to this comment
  2. Rochell Wittnebel

    Jan 30. 2011

    You really make it seem so easy with your presentation but I find this matter to be actually something that I think I would never understand. It seems too complex and extremely broad for me. I’m looking forward for your next post, I will try to get the hang of it!

    Reply to this comment
  3. Varun

    May 08. 2011

    I am sorry to tel u..explanation little absurd… ## replaces and concatenates..Whatever token(variable) comes after ## is being replaced with the corresponding argument passed into macro

    The program flow is:
    begin will be replaced with decode(a,n,i,m,a,t,e)
    decode(s,t,u,m,p,e,d) makes a = s,n = t, i = u and soo on…

    in short m##s##u##t will be replaced with
    m hence
    main

    ##concatenates with the previous token aftr replacing whtever comes aftr ##
    Hope this helps..

    Reply to this comment
  4. chandan

    Jul 29. 2011

    gud yar

    Reply to this comment
  5. suseenthiran

    Aug 11. 2011

    amazing this what i want

    Reply to this comment
  6. navpallav

    Feb 18. 2012

    awesomw! really intresting.

    Reply to this comment
  7. Suman

    Apr 28. 2012

    Amazinggggggggggggggggggggg.

    Reply to this comment
  8. fr3nZy

    Aug 01. 2012

    so can we make a self destructing c program without main function, which changes current sys date..

    Reply to this comment
  9. siddarth

    Sep 28. 2012

    It’s awesome but it won’t working in my pc. please help me

    Reply to this comment
  10. Rajaneesh

    Nov 20. 2012

    I am getting an error at begin i.e “) is expected”. Please help me

    Reply to this comment
  11. ranjith

    Jan 28. 2013

    Good Stuff

    Reply to this comment
  12. mani

    Nov 29. 2013

    nice n simply excellent

    Reply to this comment
  13. Django

    Dec 05. 2013

    Actually you can write a C program without main without the gimmick. All you have to do is compile the code first to object code and then link the code by specifying to the linker that the entry function is main.
    Remember, compiling is different than linking.
    C standards clearly state that main is not a requirement.
    Main tells your regular compiler where to start linking from. If you have compiler and linker separately then first compile all your code to object code and use linker.
    If you don’t believe me try it yourself, or check the Linux kernel source. Its a huge C program without a main()
    Th kernel starts execution from start_kernel()

    Reply to this comment

Leave a Reply