Skip to main content

Types of errors and debugging

Short answer

Errors include syntax, logical, runtime, linker, and semantic types. Debug a C program by reading compiler messages, dry‑running, inserting printf statements, using a debugger with breakpoints, and testing with normal, boundary, and invalid data.

Discuss the types of errors in programming. How do you debug a C-program?

Types of errors

  1. Syntax errors: violation of the language rules, such as a missing ; or misspelled keyword. Detected by the compiler; the program does not compile.
  2. Logical errors: the program runs but gives wrong output because the logic is wrong, such as using + instead of *. No error message is shown.
  3. Runtime errors: occur during execution, such as division by zero, invalid memory access or opening a missing file. The program stops abnormally.
  4. Linker errors: a function used is not found when linking, such as a misspelled prntf giving "undefined reference".
  5. Semantic errors: statements that follow the grammar but have no valid meaning, such as a + b = c; (a value cannot be assigned to an expression). The compiler reports them.

Debugging a C program

  • Read the compiler's error and warning messages (compile with gcc -Wall) and fix the first error first, as one error can cause many.
  • Trace the program by hand with sample data (dry run) and compare with the expected result.
  • Insert printf() statements to display variable values at key points.
  • Use a debugger (such as gdb or the IDE debugger) to set breakpoints, step through statements and watch variables.
  • Divide the program into functions and test each separately.
  • Test with normal, boundary and invalid data.

✓ Answer checked by Claude Opus 5.5 (high effort)

All Computer Programming chapter 2 questions →