01 / 20

Build vs. Runtime
Errors in C++

Learn to distinguish between errors the compiler catches before your program runs and errors that crash your program while it executes.

C++

What Is a Programming Error?

Every C++ program you write goes through two major phases: building (compiling and linking) and running (execution). Errors can appear in either phase, and each type demands a different debugging strategy.

Build Phase
The compiler translates your source code into machine code. Errors here stop the build entirely.
Run Phase
Your compiled program executes. Errors here cause crashes, wrong output, or unexpected behavior.

The Build and Run Pipeline

Source Code Compiler Build Error ✕ Fails ✓ OK Linker Executable (.exe) Running Runtime Error

What Are Build Errors?

Build errors occur before your program ever runs. The compiler reads your source code and checks that it follows C++ grammar rules. If something violates the language rules, compilation fails with an error message.

1
Syntax errors: Missing semicolons, unmatched braces, misspelled keywords.
2
Type errors: Assigning a string to an integer variable, calling a function with wrong argument types.
3
Undeclared identifiers: Using a variable or function name that was never declared.
4
Linker errors: Calling a function declared but never defined, or missing library references.

What Are Runtime Errors?

Runtime errors happen after your program compiles successfully and starts executing. The code is grammatically correct, but it does something illegal or unexpected while running.

1
Division by zero: Dividing an integer by a variable that happens to be 0.
2
Out-of-bounds access: Accessing an array index that doesn't exist.
3
Null pointer dereference: Following a pointer that points to nothing.
4
Stack overflow: Infinite recursion exhausting available memory.

The Core Rule

Build Error Detected by the compiler / linker No executable produced Easier to find ✓ Runtime Error Detected during program execution Program compiles fine Harder to find ✕

Build vs. Runtime: Key Distinction

Build Error Runtime Error
When detected Before execution During execution
Detected by Compiler / Linker Operating system / program logic
Executable created? No Yes
Example Missing semicolon Division by zero
Difficulty to fix Usually straightforward Can be tricky

How to Debug Each Type

1
Read the error message carefully. Build errors include the file name, line number, and a description. Start there.
2
Fix the first error first. One build error can cascade into many false errors below it. Fix the top one and recompile.
3
For runtime errors: add output statements. Print variable values before the crash to narrow down the problem.
4
Check edge cases. Does your code handle zero inputs, empty arrays, or negative numbers?
5
Use a debugger. Tools like GDB or your IDE's built-in debugger let you step through code line by line.

Example 1: Missing Semicolon

#include <iostream> int main() { int x = 10 std::cout << x << std::endl; return 0; }
error: expected ';' after expression int x = 10 ^

Example 2: Undeclared Variable

#include <iostream> int main() { int score = 95; std::cout << Score << std::endl; return 0; }
error: use of undeclared identifier 'Score' std::cout << Score << std::endl; ^~~~~

Example 3: Division by Zero

#include <iostream> int main() { int total = 100; int count = 0; int average = total / count; // danger! std::cout << average << std::endl; return 0; }
Compiles successfully ✓
Floating point exception (core dumped)

Example 4: Array Out of Bounds

#include <iostream> int main() { int arr[3] = {10, 20, 30}; std::cout << arr[5] << std::endl; return 0; }

Example 5: Linker Error

#include <iostream> void greet(); // declared but never defined int main() { greet(); return 0; }
undefined reference to 'greet()' collect2: error: ld returned 1 exit status

Common Mistakes

✕ Confusing the two types
Saying "my program crashed so there's a compile error." If it ran at all, the build succeeded: the error is at runtime.
✕ Ignoring warnings
Compiler warnings aren't errors, but they often predict runtime problems. Always fix warnings: use -Wall flag.
✕ Fixing errors bottom-up
One syntax error can cause a cascade of 20+ error messages. Always fix the first reported error and recompile.

Summary Comparison

Feature Build Error Runtime Error
Timing Compilation / Linking Execution
Cause Violates C++ grammar or type rules Illegal operation at runtime
Can run program? No Yes, but may crash
Error message from Compiler / Linker OS or no message at all
Examples Missing ; undeclared variable Divide by 0, array out of bounds
Debugging strategy Read error output, fix line Print values, use debugger

Question 1 of 5

A student writes a program but the compiler outputs: "error: expected ';' before 'return'". What type of error is this?

Question 2 of 5

A program compiles without errors but crashes with "Segmentation fault" when the user enters certain input. What type of error is this?

Question 3 of 5

What happens when the student tries to build this program?

Question 4 of 5

This code compiles without errors. When it runs, what is the most likely issue?

Question 5 of 5

Which of the following would the compiler catch as a build error?