Nirmani warakaulla
5 min readJul 5, 2021

When you write a C program using GCC compiler, It is pretty simple. Right?

Have you ever wondered what happens during the compilation process and how the C program gets converted to an executable file?

There are four main stages through which a source code passes in order to finally become an executable.

In this post, I’ll walk through each compiling stage: Preprocessing, compilation, assembly, and linking.

The Four Stages of Compiling a C Program

My program is very simple code. Print ”Hello, World”.

Before going through any of the activities, let’s take a quick look at how to compile and run a ‘C’ code using GCC, using the above simple hello world example.

1st step: You should check your GCC compiler on the terminal.

gcc -v + Enter key

2nd step: Clear terminal (clear + Enter key).

Get a notepad and write the code ( touch “ hello.c +Enter key “ )

3rd step: Open that hello. c text file and type the hello world program code and save it.

Now you can see your hello.c file on your home directory.

4th step: Compile your hello world program. ”test “ is the executable file. It is a binary file.

5th step: Then you can run the executable file” test” ( ./test + Enter key )

And you can see your program. ”hello, world “ .

This is a simple hello world program on how to compile and how to execute.

Here we go to explain the multi-stage process of this c program.

While compiling hello. c the gcc compiler reads the source file hello. c and translates it into an executable file called test. The compilation is performed in four sequential steps by the compilation system. A collection of four stages are preprocessor, compiler, assembler, and linker.

Now, let’s perform all four stages to compile and run the C program one by one.

1. Preprocessor

1st step: change your working directory to desktop. ( cd Desktop + Enter key)

Get your notepad on the terminal you can use gedit.( gedit + Enter key )

2nd step: Type your hello world c program on the notepad and save it as “hello. c” on the Desktop. Then you can see your “hello. c” file on the desktop screen.

3rd step: you can only preprocess your program with the command gcc -E

gcc -E hello.c + Enter Key

You can see these command lines below. During the compilation of a C program the compilation is started off with preprocessing the directives (e.g., #include and #define).

you can see our hello world program at bottom of the code.

Or you can run this code

cpp hello.c > hello.i + Enter key

The result is a file hello. i that contains the source code with all macros expanded. If you execute the above command in isolation, then the hello.i file will be saved to disk and you can see its content by vi or any other editor you have on your Linux.

Preprocessor removes comments, includes header files in source code, and replaces the macro name with code.

2. Compiler

4th step: After preprocessing only step you can do for preprocessing and compiling to assembly only. It can do with gcc -s command

gcc -s hello.c + Enter Key

The compiler translates hello.i into hello.s. File hello.s contains assembly code.

The command-line option -S says the compiler to convert the preprocessed code to assembly language without creating an object file.

Then you can see the hello.s file on the desktop.

When you are looking at assembly code you may note that the assembly code contains a “call” to the external function printf.

3. Assembler

The assembler translates hello.s into machine language instructions and creates an object file into hello.o

gcc hello.c -o hello.o + Enter key

Then you can see hello.o file on the Desktop.

Try to open it.

The above command will generate hello. o as it is specified with the -o option. And, the resulting file contains the machine instructions for the classic “Hello World!” program, with an undefined reference to printf.

4. Linker

5th Step: Now compile your hello. c file

This is the final stage in the compilation of the “Hello World!” program. This phase links object files to produce the final executable file

Then you can see the “a.out” file on Desktop.

6th step: Execute that “a.out“ file. then you can see the output of your program “hello world” on the terminal.

Nirmani warakaulla
Nirmani warakaulla

Written by Nirmani warakaulla

Experienced UI/UX engineer deeply committed to creating impactful solutions through innovative design.

No responses yet