externvoid_exit(registerint);int_start(){&am"/> externvoid_exit(registerint);int_start(){&am">

Home  >  Article  >  Backend Development  >  How to write a running C code without main() function?

How to write a running C code without main() function?

PHPz
PHPzforward
2023-09-07 16:25:02926browse

How to write a running C code without main() function?

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

Example

#include <stdio.h>
extern void _exit(register int);
int _start() {
   printf("Program without main</p><p>");
      _exit(0);
}

Output

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!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete