Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to write a running C code without main()?
In C programming, it is possible to write a program without the traditional main() function. While main() appears to be the entry point from a programmer's perspective, the system actually calls _start() first, which sets up the environment before calling main().
Syntax
int _start() {
// Program code here
_exit(status_code);
}
Note: To compile programs without main(), use the
-nostartfilesflag with gcc to skip the standard startup files that expect a main() function.
Example
Here's how to create a C program using _start() instead of main() −
#include <stdio.h>
extern void _exit(register int);
int _start() {
printf("Program without main<br>");
_exit(0);
}
Program without main
How It Works
The _start() function serves as the actual entry point for the program execution. Key points about this approach:
- The
_start()function bypasses the standard C runtime initialization - You must use
_exit()instead ofreturnto terminate the program properly - The
-nostartfilescompiler flag tells GCC to skip linking standard startup files - This approach is system-dependent and may not work on all platforms
Key Points
- This technique is primarily used for system-level programming and embedded systems
- Standard library functions may not work correctly without proper runtime initialization
- Always use
_exit()to terminate the program, notreturn
Conclusion
While it's technically possible to write C programs without main() using _start(), this approach bypasses standard initialization and should only be used for specialized system programming tasks.
