What happens when you compile your first file in C

Cynthia Domínguez
3 min readFeb 10, 2019

--

The C programming language is a widely-used imperative computer programming language that must use a compiler. C provides low-level access to memory. The description “low-level” refers to the language’s distance to the machine language of ones and zeros. Many popular languages such as Python, Ruby, Java, and JavaScript are build on the fundamentals of the C language. I happen to like learning Romance languages, so I tend to think of C as the Latin of computer languages.

Because of this low-level nature, programs written in C need to go through a compiler, which involves four steps: Preprocessing, Compiling, Assembly, and Linking.

Preprocessing

The preprocessor generates an intermediate file that is ready for assembly. This takes out any commented language, includes header files in the source code, and replaces macro names with code. A file is generated after this process.

Use the command above to create a file showing the preprocessing stage of compilation.
The hello.c file at the preprocessed stage of compilation.

Compiling

At the compiling stage, the preprocessed language is converted to assembly language, which are still English words and mnemonics.

Use the command above to create a file showing the assembly language after compiling.
The hello.c file in assembly language.

Assembly

The assembler then converts the assembly code to object code, which is in binary format. Several files of object codes are generated after this process.

Use the command above to create a file showing the object code after assembly.
A snippet of the hello.c file after the assembly stage.

Linking

The files generated are made of machine instructions, but are out of order. To make an executable file, these files need to be rearranged and instructions for library functions will be added, then finally packaged into an executable file.

Using gcc, which will add in the final stage of linking. Finally, create an output file called ‘hello’ and run it!

The Gnu Compiler Collection (GCC) is the standard compiler for most Unix systems, and is free, thanks to the Free Software Foundation.

If you want to try seeing the four stages of compiling for yourself, follow the steps, as I outlined above in more detail, from Calle Erlandsson.

--

--

Cynthia Domínguez
0 Followers

Software engineer focusing on accessibility, information security, and privacy.