C Program Without Main() Function

Have you ever thought that a C program can be written without the main() function? It’s a quite interesting question.

We are taught in almost every book that main() is the entry point of program execution. So, every program must have a main somewhere. main will usually call other functions to help perform its job, some that we wrote, and others from libraries that are provided for us. Logically it’s impossible to write a C program without main() function.

It’s true that a C program can’t be written without main() function. There are some methods given below, but truth is that in every method, main() function is hidden or used in an indirect way.

Now, look at some examples given below.


C Program Without Main() Function using preprocessor

#include<stdio.h>
#define fun main
int fun()
{
  printf("Hello, World.\n");
  return 0;
}

In this program, we used pre-processor directive #define with arguments to give an impression that the program runs without main. Pre-processor is a program which processes the source code before compilation.


C Program Without Main() Function using Token-pasting Operator (##)

#include<stdio.h>
#define fun m##a##i##n
int fun()
{
  printf("Hello, World.\n");
  return 0;
}

In this program, we Used Token-pasting Operator. The ‘##’ operator is called the token pasting or token merging operator. So, the “m##a##i##n” becomes “main”.


C Program Without Main() Function using Macro and Token-pasting Operator (##)

#include<stdio.h>
#define fun(c,o,d,e,x,a,m,p,l) d##c##l##a
#define show fun(a,b,m,d,e,n,g,h,i)
int show()
{
  printf("Code Example.\n");
  return 0;
}

Explanation:-

#define fun(c,o,d,e,x,a,m,p,l) d##c##l##a

The macro fun(c,o,d,e,x,a,m,p,l) is being expanded as “dcla”, the token pasting operator(##) merges d, c, l & a into “dcla”. When we pass macro fun(c,o,d,e,x,a,m,p,l) as argument then it merges the 3rd, 1st, 9th and the 6th characters(tokens) respectively. Note that in fun(c,o,d,e,x,a,m,p,l) no characters(tokens) are repeted. Now in next line of program,

#define show fun(a,b,m,d,e,n,g,h,i)

The preprocessor replaces the macro “show” with the expansion fun(a,b,m,d,e,n,g,h,i). According to the macro definition in the previous line the argument must be expanded so that the 3rd, 1st, 9th and the 6th characters respectively must be merged. In the argument fun(a,b,m,d,e,n,g,h,i) 3rd, 1st, 9th and the 6th characters are m, a, i & n. So, the next line “int show” is replaced with “int main” by preprocessor before the program is passed to the compiler.


Another Method

//Compile this program with gcc -nonstartfiles
void _start()
{
  int fun=my_main();
  exit(fun);
}
int my_main()
{
  puts("Program without main function.\n");
  return 0;
}

Save it as: – program.c
Compile it: – gcc -0 Program program.c -nonstartfiles
Run it as: – ./Program

The Compiler will give a warning but run the executable file, it will work fine in GCC compiler on Linux platform.

The symbol _start is the entry point of your program. That is, the address of that symbol is the address jumped to on program start. Normally, the function with the name _start is supplied by a file called crt0.o which contains the startup code for the C runtime environment. It sets up some stuff, populates the argument array argv, counts how many arguments are there, and then calls main. After the main returns, the exit is called.

Source:- https://stackoverflow.com/a/29694977/417501

Final Words:- So actually C program can never run without a main() function. We are just disguising the main() with the pre-processor. But actually, there exists a hidden main function in the program.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *