Computers don’t understand human languages like Python or C. They only understand binary machine code—a sequence of ones and zeros. So how does our human-readable source code get turned into something a CPU can execute? This is where we see the magic of compilers. A compiler is a special program that translates source code from one language into another, typically into machine code.
Table of Contents
💻 Compilation vs. Interpretation
Programming languages are generally either compiled or interpreted:
- In a compiled language (like C, C++, Rust), a compiler processes your entire source code ahead of time, creating a standalone binary executable file (e.g., an `.exe` on Windows). This file can then be run directly by the operating system. This process generally results in faster programs.
- In an interpreted language (like Python, JavaScript), an interpreter reads and executes your code line by line at runtime. There is no separate compilation step. This makes development faster and more flexible but can result in slower execution.
💻 The Compilation Process: A C Example
Let’s look at what the GNU Compiler Collection (GCC) does with a simple C program. The process has several stages:
- Pre-processing: The compiler first handles directives like `#include
`. It finds the `stdio.h` file and inserts its contents directly into your source code. - Compilation to Assembly: The pre-processed code is then translated into assembly language. This is a human-readable representation of the raw CPU instructions. For example, a `puts(“Hello”)` command might become `movl` and `call puts` instructions.
- Assembling to Machine Code: An assembler then converts the assembly language instructions into their binary equivalents (machine code), creating an ‘object file’.
- Linking: Finally, a linker takes your object file, combines it with code from any libraries you used (like the standard I/O library), and produces the final binary executable.
More Topics
- Python Project Guide: NumPy & SciPy: For Science!
- Python Project Guide: Times, Dates & Numbers
- Python Project Guide: Making Scripts
- Python Project Guide: Build an FTP Client & Server
- Python Project Guide: Using Functions in Python 3
- Python Project Guide: How to Get Started with Python 3
- Coding Concepts Explained: Avoid Common Coding Mistakes