The Compilation Process of C Language

Learn how C code is converted into machine code that can be executed by a computer.

Ishan Hansaka Silva
LinkIT
2 min readSep 5, 2023

--

photo by author

What is compilation?

Compilation is the process the computer takes to transform a program written in a high-level language from source code into machine code that the computer can understand.

Source code

Sour code which appears in a human-readable text format includes instructions that guide the program’s execution.

An example of printing Hello World

#include<stdio.h>
int main()
{
printf("Hello World"):
return 0;
}

4 stages of the compilation process

photo by author

Stage 1 — Preprocessing

The preprocessor is a program that is used to process source code before it is compiled. It performs several tasks, including:

  • Removing comments
  • Expanding macros
  • Including files
  • Converting source code to expanded code.

Output file: hello.i

Stage 2 — Compiling

The C compiler checks for valid syntax in C language. If any wrong syntax is found, the compiler reports that error. After the expanded code has been syntactically verified, the compiler converts it into assembly code.

Output file: hello.s

Stage 3 — Assembling

In this stage, the assembler converts assembly code into pure binary code or machine code known as the object code.

Output file: hello.o

Stage 4 — Linking

The linker merges all the object code from multiple modules and libraries into a single file and produces the final executable file.

Output file: hello.exe

photo by shraddhacompnotes

References

  • Does the executable file depend on OS?

Are executable files (.exe) generated by compilers machine specific and/or operating system specific? In other words, can .exe files be ported from one machine to another, having the same (or different) OS? — Quora

  • Do assembly languages depend on OS?

How do assembly languages depend on operating systems? — Stack Overflow

  • Are compilers only operating system dependent and not hardware dependent?

https://www.quora.com/Why-are-compilers-only-operating-system-dependent-and-not-hardware-dependent

  • Do all programming languages compile to the same machine code?

Do all programming languages compile to the same machine code? — Quora

--

--

Ishan Hansaka Silva
LinkIT

Undergraduate at University of Moratuwa Faculty of Information Technology Technical Writer at LinkIT Publication https://www.linkedin.com/in/ishanhansakasilva/