Back to C++
2026-05-085 min read

C++ Errors

Learn C++ Errors step by step with clear examples and exercises.

Title: Mastering C++ Errors: Understanding and Avoiding Common Mistakes (1023 words)

Why This Matters

C++ is a powerful programming language with numerous applications in various domains. However, its complex syntax and error-prone nature can make it challenging for beginners to master. In this full guide, we delve into common C++ errors that developers face and provide strategies to avoid them, enabling you to write cleaner, more efficient code. Whether you're preparing for an exam, interview, or real-world coding projects, understanding C++ errors is crucial.

Prerequisites

To benefit from this tutorial, you should have a basic understanding of the C++ programming language, including variables, data types, functions, control structures such as loops and conditional statements, and fundamental concepts like pointers and references. Familiarity with a text editor or Integrated Development Environment (IDE) is also required to write, compile, and run C++ programs.

Core Concept

C++ errors can be classified into two categories: compile-time errors and runtime errors. Compile-time errors are detected by the compiler during the compilation process, while runtime errors occur when the program is running and cause unexpected behavior or crashes. This section will explore some common C++ errors from both categories in detail.

Compile-Time Errors

  1. Syntax Errors: These are mistakes in the syntax of your code, such as misspelled keywords, incorrect punctuation, or mismatched braces. For example:
int a = 10; // Correct
int a= 10; // Syntax error: missing semicolon
  1. Type Errors: These occur when you try to assign an incompatible data type to a variable or perform an operation between incompatible types. For example:
int x = 5;
float y = x / 2.0f; // Correct: performing division with a float to preserve decimal part
int z = x / 2; // Type error: integer division truncates the decimal part
  1. Undefined Variables: These are variables that have not been declared or initialized before being used in your code. For example:
int main() {
int x;
cout << x; // Undefined variable error: x has not been initialized
}

Runtime Errors

  1. Segmentation Faults: These occur when the program tries to access memory it shouldn't, such as an uninitialized variable or an array index out of bounds. For example:
int arr[3] = {1, 2};
cout << arr[3]; // Runtime error: segmentation fault
  1. Null Pointer Dereference: This happens when you try to access memory through a null pointer. For example:
int* ptr = nullptr;
cout << *ptr; // Runtime error: null pointer dereference
  1. Infinite Loops: These occur when a loop condition never becomes false, causing the program to hang or crash. For example:
for (int i = 0; i < 10; i++) {
cout << "Hello, World!"; // Infinite loop error: no break statement
}

Worked Example

In this section, we'll walk through a worked example that demonstrates common C++ errors and how to fix them.

#include <iostream>
using namespace std;

int main() {
int arr[3] = {1, 2}; // Correct declaration of an array
int x = 5;
float y = x / 2.0f; // Correct: performing division with a float to preserve decimal part
int z = x / 2; // Type error: integer division truncates the decimal part
cout << arr[3]; // Runtime error: segmentation fault
int* ptr = nullptr;
cout << *ptr; // Runtime error: null pointer dereference

for (int i = 0; i < 10; i++) {
cout << "Hello, World!"; // Infinite loop error: no break statement
}

return 0;
}

Common Mistakes

  1. Forgetting Semicolons: Semicolons are crucial in C++ to separate statements. Leaving them out can lead to syntax errors.
int x = 5; // Correct
int x = 5, y = 10; // Syntax error: missing semicolon after x
  1. Incorrect Initialization: Failing to initialize variables or arrays can lead to undefined behavior and runtime errors.
int arr[3]; // Undefined variable error: array not initialized
  1. Mismatched Braces: Braces should be properly matched in C++ code, as incorrect matching can cause syntax errors and hard-to-debug issues.
if (true) {
cout << "Hello, World!"; // Correct
} int x = 5; // Syntax error: brace mismatch

Common Runtime Errors

  1. Array Index Out of Bounds: Accessing an array index that is greater than its size will result in a runtime error. For example:
int arr[3] = {1, 2};
cout << arr[3]; // Runtime error: array index out of bounds
  1. Null Pointer Dereference: Attempting to dereference a null pointer will cause a runtime error. For example:
int* ptr = nullptr;
cout << *ptr; // Runtime error: null pointer dereference
  1. Infinite Loops: Failing to include a break or exit statement in a loop can lead to an infinite loop, causing the program to hang or crash. For example:
for (int i = 0; i < 10; i++) {
cout << "Hello, World!"; // Infinite loop error: no break statement
}

Practice Questions

  1. What is the output of the following code?
int main() {
int arr[3] = {1, 2};
cout << arr[3];
return 0;
}

Answer: Runtime error: segmentation fault

  1. What is the output of the following code?
int main() {
int x = 5;
float y = x / 2.0f;
cout << y;
return 0;
}

Answer: Output will be 2.5 (float value), but the code contains a type error.

FAQ

  1. Why do I get a segmentation fault when accessing an array out of bounds?
  • A segmentation fault occurs when your program tries to access memory it shouldn't, such as an uninitialized variable or an array index out of bounds. To avoid this error, always ensure that array indices are within their valid range and initialize variables before using them.
  1. What happens if I forget a semicolon in my C++ code?
  • Forgetting a semicolon can lead to syntax errors. Semicolons are crucial in C++ to separate statements, so leaving them out can cause unexpected behavior or compile-time errors.
  1. Why does the compiler not warn me about undefined variables?
  • The C++ compiler only catches variables that are explicitly declared but not defined or initialized before they're used. However, it doesn't issue a warning if you forget to declare a variable before using it, as long as the variable is in scope and has a valid data type. In such cases, the program will run fine until the undefined variable is actually used, at which point you might encounter runtime errors or unexpected behavior.
C++ Errors | C++ | XQA Learn