Home > Backend Development > C++ > body text

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

PHPz
Release: 2023-09-07 16:25:02
forward
974 people have browsed it

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);
}
Copy after login

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$
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!