Writing a (valid) C program without main()

https://news.ycombinator.com/rss Hits: 1
Summary

Why C?Most programming languages start execution from a main() function. Go has func main(), Java has public static void main(), and C has int main(). So why did I pick C for this tutorial?C is the closest you can get to the operating system without writing assembly. The Linux kernel itself is written in C, and the system call interface that every program relies on is designed with C conventions in mind. When you learn how C programs are compiled, linked, and executed, you are learning how the Linux API actually works.A typical C program starts with a main() function. But where does main() come from? The compiler? The linker? The operating system?In this tutorial, you will walk through the entire C compilation pipeline. You will start with a normal hello world, watch the preprocessor expand a macro, watch the compiler turn C into assembly, and watch the assembler and linker turn assembly into a binary. Then you will remove main() from the picture and still produce a working executable.The playground is a vanilla Ubuntu 24.04 machine with gcc, as, ld, and friends already installed.Step 1: Hello WorldCreate a file named hello.c with the following content:#include <stdio.h> int main(void) { printf("Hello, world!\n"); return 0; } Copy to clipboardCompile it and run it:gcc hello.c -o hello ./hello Copy to clipboardYou should see the familiar greeting. Now ask yourself: how many separate tools ran when you typed that single gcc command?Behind the scenes, gcc is a driver that runs several tools in sequence:Preprocessor (cpp) - handles #include, #define, and conditional compilation.Compiler (cc1) - turns C source into assembly.Assembler (as) - turns assembly into an object file.Linker (collect2 / ld) - links object files and libraries into the final executable.The rest of the tutorial visits each of these stages.The four stages of the C compilation pipeline.Step 2: The preprocessorThe preprocessor is a text processor. It does not understand C types or control flow; it only ...

First seen: 2026-07-25 16:45

Last seen: 2026-07-25 16:45