How Are C Programs Compiled?

Programming languages need to be human-readable so we can easily use them; however, machines can only understand 0’s and 1’s. So, how does our machine understand our program?
Well, before our machine can execute our program, we need to translate it into 0’s and 1’s. Compilers and interpreters do this task for us, though we’ll only talk about compilers in this article.

In computing, a compiler is a computer program that translates computer code written in one programming language (the source language) into another language (the target language). The name “compiler” is primarily used for programs that translate source code from a high-level programming language to a lower level language (e.g. assembly language, object code, or machine code) to create an executable program.
Source: Wikipedia

When you write a program in C, you need to compile it first before you can execute it. But how exactly does compilation work in C?

Compilation Process

Compilation in C goes through four main stages:

1. Preprocessing
2. Compiling
3. Assembling
4. Linking

(For all examples, we will use the GNU compiler (GCC) with the command gcc)


The first stage of the compilation process is preprocessing. The preprocessor is a program that processes input data to produce output that is then passed on to the compiler.
In this stage, the preprocessor removes comments from the code, defines macros, and the lines starting with # are interpreted as preprocessor commands.

To see the result of the preprocessing, you can use the command gcc with the option -E:

gcc -E hello_world.c

Here is an example of the result you can get:

At this step, the source code file’s .c extension is replaced by the .i extension.


The second stage of compilation is also called compilation. The preprocessed code is translated to assembly instructions that form an intermediate human-readable language.

This step allows for C code to contain inline assembly instructions and for different assemblers to be used.

To see the result of the compilation stage, use the -S option.

gcc -S hello_world.c

This will create the file hello_world.s which contains the assembly instructions.


In this stage, the code containing the assembly instructions is translated into object code by an assembler. The output contains instructions that the machine can understand, composed of 0’s and 1's.

To do this, use -c option:

gcc -c hello_world.c

It will create a new file with a .o extension.


Although the object code contains instructions in binary, it’s still not executable because pieces of the program are missing or in wrong order. To produce an executable program, we need to rearrange those pieces and fill in the missing ones through the process called linking.
The linker does that for us.

Let’s run the gcc command without any options.

gcc hello_world.c

The result of this is an executable .exe file.

If you run the command without options, the file will be named a.out by default. If you want to give your file another name, use the -o option.

gcc -o hello_world.exe hello_world.c

That’s it. Thanks for reading!




Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

4 ways to give back to the WordPress community

6. Jenkins and Allure Report Integration

Invoice Gen(Invoice Generation and Management) using .Net Core and EF Nulled

State machine

Why People Are Reluctant to Upgrade

A Minimalistic Web Portfolio for all developers 😎

Quick guide: Free Moodle instance on AWS with domain and SSL

December 2021 Recap

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Juliette Mesnil

Juliette Mesnil

More from Medium

C Static Libraries and gcc

CS373 Spring 2022: Dinesh Krishnan Balakrishnan

Python — How To Get the String Length and Size

Hacker School Application