C Program without using the Main() Function

In C, main() function is a user-defined function. The main function acts as the beginning point for program execution. However, we are writing the program without using the main function. To call main, we use macro and token pasting operator instead of main.

Problem Description

Write a C program without using main function.

Problem Solution

1. Design a macro to obfuscate the main function.
2. Define the function being used and call the macro which then append the word main by token pasting.

There are various ways to write a C program without using main function. Let’s take a detailed look at all the approaches for writing a program without main function.

Method 1: (Using a Macro that defines Main)

A macro is a code that is substituted by the value of macro. The macro directive is defined by the #define directive.

Program/Source Code

Here is source code of the C Program without using the main function by using a Macro that defines Main. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

advertisement
advertisement
  1. /*
  2.  * C Program Using a Macro that defines Main
  3.  */
  4.  
  5. #include <stdio.h>
  6.  
  7. #define pseudo main
  8.  
  9. int pseudo(void)
  10. {
  11.     printf("Hello World\n");
  12.     return 0;
  13. }
Program Explanation

1. A macro is defined which is called pseudo.
2. The pseudo macro is then used to define the main function.
3. The pseudo macro is then called which works as the main function and displays the output.

Program Output:
Hello World

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
Method 2: (Using Token Pasting Operator)

Token Pasting is a preprocessing operator. It is primarily used to combine two tokens into one while extending macros. Token pasting is done by the ‘##‘ pre-processing operator.

Program/Source Code

Here is source code of the C Program to display function using Token Pasting operator. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C Program to display function using Token Pasting operator
  3.  */
  4.  
  5. #include <stdio.h>
  6.  
  7. #define pseudo m##a##i##n
  8.  
  9. int pseudo(void)
  10. {
  11.     printf("Hello World\n");
  12.     return 0;
  13. }
Program Explanation

1. #define is a preprocessor directive in C.
2. A macro is defined which appends the work main using token pasting in contrast to the previous example where we directly included the word main.
3. Here we declare using token pasting i.e. #define pseudo m##a##i##n.
4. Before compilation the code “m##a##i##n” will be converted to “main”.
5. The pseudo macro is then called which works as the main function and displays the output.

Program Output:
Hello World

advertisement
Method 3: (By Obfuscation using Token Pasting)

In this method, the program actually runs as main but in disguise. This is achieved by replacing the main function with a macro.

Program/Source Code

Here is source code of the C Program to display function without using the main function. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C Program to display function without using the Main Function
  3.  */
  4.  
  5. #include <stdio.h>
  6. #define pseudo(s, t, u, m, p, e, d) m##s##u##t
  7. #define nomain pseudo(a, n, i, m, a, t, e)
  8.  
  9. int nomain(void)
  10. {
  11.     printf("Hello World\n");
  12.     return 0;
  13. }
Program Explanation

1. This program actually runs main but with a disguise. This is acheived by replacing the main function with a macro. The macro here is pseudo.
2. The macro is defined as follows:
#define pseudo(s, t, u, m, p, e, d) m##s##u##t
This code tells the compiler to do token pasting. The tokens order is defined as “m##s##u##t”, next time when decode is encountered with 7 parameters, replace it by the concatenation of the 4th, 1st, 3rd and 2nd argument.
3. #define nomain pseudo(a, n, i, m, a, t, e). In this line, when the nomain function is defined as pseudo(a, n, i, m, a, t, e), the compiler expands the argument such that the 4th argument is taken first, first argument is taken second, third argument is taken third and finally comes the second argument that is “m, a, i and n” respectively. Therefore the macro decodes the argument to main. So the nomain is replaced by main.
4. Use function named nomain and print the output as “Hello World” and exit.

advertisement
Program Output:
Hello World

Conclusion:
No C program can work without main function. There has to be some way to disguise the main function as we did here by defining a macro function which then decodes itself to the word main.

To practice programs on every topic in C, please visit “Programming Examples in C”, “Data Structures in C” and “Algorithms in C”.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.