How To

Can You Write a C Program Without a Main Function ? Here is How

Have 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:

#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 “);

}

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).

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

Pin It on Pinterest

Share This