Saturday, June 16, 2012

C Program Without Main Fuction


C Program Without Main Function

C program without Main Function

 Main function call the other functions to perform there tasks. As there are Managers in Hotels. Main is the Manager of C programming.
Now a Logical Question arises.

Q.Can we Build a C Program Without Main Function?
Ans. Yes , It is possible to write a C Program without defining a Main function.

Just type the following code in your C editor. (For example Turbo C/C++):

  • #include
  • #include
  • #define decode(s,t,u,m,p,e,d) m##s##u##t
  • #define kgtricks decode(a,n,i,m,a,t,e)
  • int kgtricks()
  • {
  • clrscr();
  • printf(" C Program Without Main function");
  • getch();
  • return 0;
  • }
C Program input Source Code

  • C Program Without Main function
Output of C Program Without Main Function

Now most of you still may be wondering about the fact that how come the above program run without Main Function.

The secret is that the preprocessor directory #define line of code combined with decode work together to form a hidden Main function.

The '##' operator is known as the token pasting or token merging operator. We can merge two or more characters using it.

Preprocessor : A program which process source code before compilation.

In the Line '#define decode(s,t,u,m,p,e,d)m##s##u##t
 The macro decode(s,t,u,m,p,e,d) is expanded as "msut".
The ## operator merger m,s,u & t into msut.

 Now look at "#define kgtricks decode(a,n,i,m,a,t,e)
Here the macro kgtricks is been replaced with expansion decode (a,n,i,m,a,t,e)
According to the macro definition in the lines programmed above, the argument must be expanded so that the 4th, 1st , 3rd , and the 2nd characters must be merged . In this case (a,n,i,m,a,t,e) , the arguments are 4th, 1st , 3rd  and 2nd character. The term will show up as m,a,i,n 

Here in the third line
int kgtricks is replaced by int main by the preprocessor .

Hence it can be said that there can not be any program without a main function. This trick in the above program just hides the main function from the user. Use of preprocessor intelligently replaces the word "kgtricks" with "main"

In other words int kgtricks= int main

Also Read:

How to play Music in C ?

How to create hidden text files without any software?

Hide data behind the image -Steganography 

If You Enjoyed This, Take 5 Seconds To Share It

About Author:

Kunal Ganglani: I am a Passionate Blogger, a Computer Science Engineer, Internet Marketer and an Entrepreneur at heart. I am the founder and Admin at KGTricks.

Connect With Me @ | Facebook


  1. Hey admin,
    of course this blog is doing a very good job of serving useful information. I'm proud to be a part of its Readers community.good job dude...
    for more c programing visit my web

    1. Thanks for the compliments Hardik. We are glad to have readers like you :)


Related Posts Plugin for WordPress, Blogger...