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 -nostartfiles flag 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 of return to terminate the program properly
  • The -nostartfiles compiler 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, not return

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.

Updated on: 2026-03-15T10:37:16+05:30

8K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements