See the Tutorial List

C Program without a main() function

Below is a program without main().

In the below program, main() function is there, but hidden using the preprocessors.

As you can see in the second line, #define decode() function is used, which holds a character combination of m,a,i,n and is followed by ##m##a##i##n.

Here ## operator is used to merge the characters in the order mentioned using ##, which is main

In the 3rd line #define go decode(m,a,i,n) as we have specified the characters in same order, the decode function will assign value main for go.

#include<stdio.h>
//Need to include the following statements in same manner
#define decode(m,a,i,n) m##a##i##n
#define go decode(m,a,i,n)

int go()
{
    printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");
    printf("You have just executed your first program without making use of main() function!\n");
    printf("\n\n\t\t\tCoding is Fun !\n\n\n");
    return 0;
}

Output:

Program without main() function

We can use different words and combination here, like

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

Here as per the first line, 4th, 1st, 3rd and 2nd chcracters have to be formed into a word. When the same combination is taken out from the word animate it makes main.


Using macro to define main

#include<stdio.h>
#define go main
int go(void)
{
    printf("Welcome to Studytonight");
    return 0;
}

This is is the simplest technique, where all we have done is provided our main() function with a different name, which is set the main before the program is executed.


Using Token-Pasting operator

#include<stdio.h>
#define go m##a##i##n
int go(void)
{
    printf("Welcome to Studytonight");
    return 0;
}