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.
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.
The Build and Run Pipeline
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.
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.
The Core Rule
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
Example 1: Missing Semicolon
Example 2: Undeclared Variable
Example 3: Division by Zero
Example 4: Array Out of Bounds
Example 5: Linker Error
Common Mistakes
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
A student writes the following code:
void printSum();
int main() { printSum(); return 0; }
The function printSum is declared but never defined anywhere in the project. The student tries to compile.
What happens when the student tries to build this program?
Question 4 of 5
Consider this code:
int arr[3] = {1, 2, 3};
for (int i = 0; i <= 3; i++) {
std::cout << arr[i];
}
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?