Here we will see, can a program be written without a main function? The answer is yes. We can write a program without main() function.
In many places, we see that main() is the entry point for program execution. From a programmer's perspective, this is correct. But from a systems perspective, this isn't true. So the system first calls the _start() function to set up the environment, and then calls the main function.
To execute this program, we need to use the option "-nostartfiles".
#include <stdio.h> extern void _exit(register int); int _start() { printf("Program without main</p><p>"); _exit(0); }
soumyadeep@soumyadeep-VirtualBox:~/Cpp_progs$ gcc test_prog.c -nostartfiles soumyadeep@soumyadeep-VirtualBox:~/Cpp_progs$ ./a.out Program without main soumyadeep@soumyadeep-VirtualBox:~/Cpp_progs$
The above is the detailed content of How to write a running C code without main() function?. For more information, please follow other related articles on the PHP Chinese website!