# Compiler

> A program that translates source code into instructions a computer can run directly.

- Level: Intermediate
- Topics: Software
- URL: https://tidbit.legios.com.ar/compiler/
- In Spanish: [Compilador](https://tidbit.legios.com.ar/es/compiler/)

A **compiler** is a program that reads source code written in a language like C, Rust, Go or Swift and translates the whole thing, ahead of time, into **machine code**: the low-level instructions a specific kind of CPU executes. The result is a file you can run on its own, like the app you double-click, without the original source code or the compiler.

Along the way the compiler checks the code. If you call something that does not exist, pass text where a number is required or forget a bracket, it refuses to finish and points at the line. It also optimizes: it rearranges and simplifies the instructions so the program runs faster than a literal translation would. That is a big reason compiled programs tend to be fast.

The alternative is an **interpreter**, which reads the code and runs it on the spot, as Python and JavaScript traditionally do. In practice the line is blurry. Java and C# compile to an intermediate form that a virtual machine runs, and browsers compile JavaScript to machine code while the page is running (**just-in-time compilation**). Because machine code is specific to a processor family, the same program has to be compiled separately for, say, Intel and Arm chips.

## An analogy

A translator who receives a whole book, translates it carefully, checks it for mistakes and hands you a finished edition. An interpreter is more like a live interpreter, translating sentence by sentence as the speaker talks.

## Keep in mind

- Translates source code into machine code before the program runs.
- Catches many mistakes early and refuses to build broken code.
- Optimizes the result, so compiled programs are often fast.
- Machine code is tied to a CPU family, so builds differ for Intel and Arm.

## Go deeper

- [Compile (MDN glossary)](https://developer.mozilla.org/en-US/docs/Glossary/Compile)
- [Compiler on Wikipedia](https://en.wikipedia.org/wiki/Compiler)
- [Interpreter (computing) on Wikipedia](https://en.wikipedia.org/wiki/Interpreter_(computing))

## Related terms

- [Source code](https://tidbit.legios.com.ar/source-code/): The human-readable text that programmers write and that a program is built from.
- [CPU](https://tidbit.legios.com.ar/cpu/): The chip that runs your programs, one instruction after another, very fast.
- [Python](https://tidbit.legios.com.ar/python/): A popular programming language known for being easy to read and quick to write.
- [JavaScript](https://tidbit.legios.com.ar/javascript/): The programming language that runs inside web pages and makes them interactive.
- [Virtual machine](https://tidbit.legios.com.ar/virtual-machine/): A complete computer made of software, running inside a real one.
- [Web browser](https://tidbit.legios.com.ar/web-browser/): The app that fetches web pages from the internet and draws them on your screen.
- [Intel](https://tidbit.legios.com.ar/intel/): The American company behind most PC and server processors for decades.
- [Arm](https://tidbit.legios.com.ar/arm/): The British company that designs the processor blueprints inside almost every phone.
