The 4 Steps of C compilation

Holden Grissett
Oct 13, 2016 · 2 min read

C is a compiled programming language — that means that it is interpreted by the machine at ‘compile time’ instead of upon execution.

This gives C certain advantages over modern programming languages. C is faster at more tasks it is programmed for because there is no need to interpret the code again after compilation. It also means that C is a smaller library, and therefore more portable than other languages.

What exactly happens when C is compiled by the compiler program? Under the metal, what is happening to make C, or any compiled language, understandable by the computer?

It seems mystical and spooky what goes on inside things like this, but in reality it only takes 4 steps to go from C code to compiled code.

To demonstrate these steps I’m going to be using the gcc (GNU Compiler Collection) command to compile. Every compiler handles these steps, but may vary slightly in what they do during them.


Step 1 — Preprocessor

The preprocessor’s job is to take the source code and generate an output based on the source code and the local macros. The output is then sent to the compiler.


Step 2 — Compiler

The compiler step is what changes the code into assembly code that your machine can understand. The output of the compiler is assembly code, which is piped to the next step.


Step 3 — Assembler

The assembler is what assembles the assembly code, which turns it into 1’s and 0’s in sequence that look like this: 0101000 01001110 . When the assembler finishes, the output is sent to the linker.


Step 4 — Linker

The linker is the part that links together your source code and the libraries that were specified at your header. The parts needed from the libraries are combined with your compiled code to produce one wrapped up package of output.


That’s it! The magic of the compiler: demystified!

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade