Bytebook Compiler

Problem Solving

From Code to Running Program

How source code becomes a running program: compilers vs interpreters, the build steps, IDEs and editors, and the three kinds of errors — syntax, runtime and logical — with debugging strategies.

4 min read2 runnable examples4-question quiz

Programming languages

Computers only execute machine code — binary instructions specific to a CPU. Programmers write in high-level languages like C, Python and Java that are readable by humans, and a translator converts them.

Level Example Who reads it
Machine code 10110000 01100001 the CPU
Assembly mov al, 0x61 low-level programmers
High-level x = 97 everyone

Compilers vs interpreters

Compiler Interpreter
Translates the whole program before it runs one statement at a time while running
Output a standalone executable file no separate file
Errors all syntax errors reported before running reported when the line is reached
Speed the program runs fast slower
Examples C, C++, Rust, Go Python, JavaScript (with JIT compilers today), Ruby

Java and Python actually do both: they compile to an intermediate bytecode, which a virtual machine then runs.

The C build pipeline

Terminal
gcc -E hello.c -o hello.i     # 1. preprocess: expand #include and #define
gcc -S hello.i -o hello.s     # 2. compile: C to assembly
gcc -c hello.s -o hello.o     # 3. assemble: assembly to machine code (object file)
gcc hello.o -o hello          # 4. link: add library code (printf) into an executable
./hello                       # 5. load and run

Normally one command does it all: gcc hello.c -o hello. See C Introduction for the diagram.

Editors and IDEs

  • A text editor (VS Code, Sublime, Vim) edits code; extensions add colours, suggestions and error checks.
  • An IDE (integrated development environment) bundles an editor, compiler, debugger and project tools: CLion and Visual Studio for C/C++, PyCharm for Python, IntelliJ for Java.
  • Online compilers (like this site's Compiler) need nothing installed.

The three kinds of errors

1. Syntax errors

The code breaks the language's grammar, so it will not compile (or start). The compiler tells you the line.

C
int main(void) {
    printf("Hello")      // missing semicolon -> error: expected ';'
    return 0;
}

2. Runtime errors

The program compiles but crashes or misbehaves while running: dividing by zero, reading past an array, opening a missing file, running out of memory.

Python
marks = [78, 85, 92]
total = 0
for i in range(4):          # one too many
    total += marks[i]
print(total)
Output
Traceback (most recent call last):
  File "main.py", line 4, in <module>
    total += marks[i]
             ~~~~~^^^
IndexError: list index out of range

3. Logical errors

The program runs and finishes, but gives the wrong answer. No error message appears — these are the hardest to find.

Python
marks = [78, 85, 92]
average = sum(marks) / 2       # bug: should divide by len(marks)
print("Average:", average)
Output
Average: 127.5
Error type When found Who finds it Example
Syntax compile time the compiler missing ; or :
Runtime while running a crash or exception divide by zero
Logical after checking the output you, by testing wrong formula

Debugging strategies

  1. Read the error message from the top — it names the file, line and problem.
  2. Reproduce the bug with the smallest input that triggers it.
  3. Print values at key points (printf / print debugging).
  4. Use a debugger: set breakpoints, step line by line, watch variables (gdb, VS Code debugger).
  5. Rubber-duck: explain the code line by line to someone (or a rubber duck). Often you spot the bug while explaining.
  6. Check edge cases: 0, 1, negative numbers, empty input, the largest input.

Test yourself

4 questions · pick an answer to check it instantly

  1. 1

    Which translates an entire program into an executable before running it?

  2. 2

    A program prints an average of 127.5 for marks 78, 85 and 92. What kind of error is this?

  3. 3

    Accessing the 11th element of a 10-element Python list is a...

  4. 4

    Which build step combines your object file with library code like printf?