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.
Write a C program without using main function.
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.
A macro is a code that is substituted by the value of macro. The macro directive is defined by the #define directive.
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.
/*
* C Program Using a Macro that defines Main
*/
#include <stdio.h>
#define pseudo main
int pseudo(void)
{
printf("Hello World\n");
return 0;
}
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.
Hello World
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.
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.
/*
* C Program to display function using Token Pasting operator
*/
#include <stdio.h>
#define pseudo m##a##i##n
int pseudo(void)
{
printf("Hello World\n");
return 0;
}
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.
Hello World
In this method, the program actually runs as main but in disguise. This is achieved by replacing the main function with a macro.
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.
/*
* C Program to display function without using the Main Function
*/
#include <stdio.h>
#define pseudo(s, t, u, m, p, e, d) m##s##u##t
#define nomain pseudo(a, n, i, m, a, t, e)
int nomain(void)
{
printf("Hello World\n");
return 0;
}
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.
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”.
- Apply for C Internship
- Check C Books
- Watch Advanced C Programming Videos
- Practice Computer Science MCQs
- Check Computer Science Books