Compiler

A compiler is a program that translates source code into a separate, runnable form ahead of time, before any of it runs.


A computer program is written in a language people can read, but a computer only understands binary. Something has to translate between the two. A compiler is one kind of translator: it takes the whole program and converts it in advance, producing a separate file the computer can run on its own.

How a Compiler Runs Code#

A compiler works in a separate step before the program runs, often called building. It reads the entire program, checks it, and converts it into a new file. Running the program then means running that produced file, not the original source code.

source code  →  compiler  →  built file  →  runs

Because the whole program is translated in advance, many errors are caught at build time, before the program runs at all. The trade-off is that the program must be rebuilt after every change to the source code.

Compiler or Interpreter#

A compiler is one of two common kinds of code translator. The other is an interpreter, which does not build the program ahead of time but instead reads and runs it one statement at a time.

The trade-off is speed against immediacy. A compiler does its work up front, so the finished program tends to run faster, but it must be rebuilt after every change. An interpreter skips the build step, so code can be changed and run straight away, at the cost of translating again on every run.

Languages are often described as “compiled” or “interpreted”, but this describes how a language is usually run, not a fixed rule. Some languages use a mix of both.

⚠️ Mistakes

Thinking the compiler is part of the program

The compiler is not the code, and it is not part of the finished program. It is a separate tool used to build the program. Once the program is built, it runs without the compiler being involved.

Forgetting to rebuild after changing the code

A compiler translates the source code at build time, not while the program runs. If the code is changed but not rebuilt, the previously built version runs instead, still showing the old behaviour.

Expecting to see errors only when the program runs

Because a compiler checks the whole program before it runs, many errors appear at build time rather than during execution. A program that will not build has not produced anything to run yet.

📚 Resources