Tamil Technicians – C Programming Course · Error Types & Debugging
Error Types in C – Compile Time, Runtime, Logical Errors & Debugging Tips
Every C programmer – from beginner to expert – faces errors and bugs. Errors are not a sign of failure; they are feedback from the compiler or from the program itself.
In this lesson, you’ll learn:
- The main types of errors in C: compile-time, runtime, logical (and warnings).
- How to read and understand compiler error messages.
- Common real-world examples: missing semicolon, divide by zero, array out of bounds, wrong formulas.
- Practical debugging techniques: printf debugging, breaking problems into smaller parts, using a debugger, tracing values step by step.
- A simple, repeatable debugging checklist for students and technicians.
1. Why Understanding Error Types Matters
When you build a circuit and it doesn’t work, you use a multimeter, check voltages, and trace signals. Similarly, when a C program fails, you need to know:
- Where the problem is: during compilation, during execution, or in the formula/logic.
- What kind of error it is: syntax, missing file, wrong pointer, infinite loop, etc.
- How to systematically debug and fix it without guessing randomly.
Most C errors fall into three main groups:
- Compile-time errors – caught by the compiler before the program is run.
- Runtime errors – occur while the program is running.
- Logical errors – the program runs, but gives wrong output.
2. Compile-Time Errors (Syntax & Semantic Errors)
Compile-time errors are reported by the compiler (e.g., GCC) when it tries to translate your
.c file into machine code. If compile-time errors exist, the compiler usually does not
generate an executable.
Syntax Errors
Violating C grammar rules: missing semicolons, wrong braces, invalid keywords.
Type Errors
Incompatible data types, wrong arguments to functions, invalid pointer conversions.
Missing Declarations
Using variables or functions without proper declaration or header file.
2.1 Syntax error examples
#include <stdio.h>
int main() {
int a = 10
printf("a = %d\n", a);
return 0;
}
Here, the line int a = 10 is missing a semicolon. Compiler message (approx):
error: expected ';' before 'printf'
2.2 Type mismatch example
#include <stdio.h>
int main() {
int a = 10;
float b = 5.5f;
printf("Sum = %d\n", a + b); // wrong format specifier
return 0;
}
Many compilers show a warning here:
warning: format '%d' expects argument of type 'int',
but argument 2 has type 'double'
2.3 Undeclared identifier example
#include <stdio.h>
int main() {
total = 10; // 'total' never declared
printf("%d\n", total);
return 0;
}
Compiler error:
error: 'total' undeclared (first use in this function)
3. Runtime Errors – Crashes & Abnormal Behaviour
Runtime errors happen when the program is running. The code compiles successfully, but something goes wrong while executing: divide by zero, invalid memory access, file not found, etc.
Divide-by-zero
Division where denominator becomes zero due to user input or logic.
Segmentation fault
Invalid memory access (null pointer, out-of-bounds array index).
File errors
Trying to read from or write to a file that cannot be opened.
3.1 Divide-by-zero example
#include <stdio.h>
int main() {
int energy, units;
printf("Enter total energy and units: ");
scanf("%d %d", &energy, &units);
int perUnit = energy / units; // danger if units == 0
printf("Per unit = %d\n", perUnit);
return 0;
}
If the user enters units = 0, the program may crash or show undefined behaviour.
3.2 Array out-of-bounds example
#include <stdio.h>
int main() {
int a[5];
int i;
for (i = 0; i <= 5; i++) { // error: should be i < 5
a[i] = i * 10;
}
return 0;
}
The loop writes to a[5], which is outside the valid range (0–4). Sometimes this causes
a crash (segmentation fault), sometimes it silently corrupts memory, producing strange bugs later.
3.3 Null pointer dereference example
#include <stdio.h>
int main() {
int *ptr = NULL;
printf("%d\n", *ptr); // dereferencing NULL -> runtime crash
return 0;
}
Accessing memory through a NULL pointer is a classic runtime error.
4. Logical Errors – Program Runs but Output Is Wrong
Logical errors are the most dangerous ones because:
- The program compiles successfully (no compile-time error).
- The program runs without crashing (no runtime error).
- The output is wrong – your calculation or decision logic is incorrect.
4.1 Wrong formula example – average
#include <stdio.h>
int main() {
int a = 10, b = 20, c = 30;
float avg;
avg = a + b + c / 3; // logical error in formula
printf("Average = %.2f\n", avg);
return 0;
}
According to C’s precedence rules, this is:
a + b + (c / 3)
instead of:
(a + b + c) / 3
4.2 Off-by-one error in loops
#include <stdio.h>
int main() {
int i;
for (i = 1; i <= 10; i++) {
printf("%d ", i);
}
// Suppose we wanted to print only 1 to 9 – loop condition is off by one.
return 0;
}
Off-by-one errors appear very often in loops and array indexing.
4.3 Wrong comparison operator
if (voltage = 230) { // assignment, not comparison
printf("Voltage is 230V\n");
}
This assigns 230 to voltage instead of comparing. The condition becomes true for all cases.
The correct version is:
if (voltage == 230) {
...
}
5. Warnings vs Errors – Why Warnings Matter
Compilers usually distinguish between:
- Errors – program will not compile.
- Warnings – program compiles, but compiler suspects a bug.
- “Format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘float’”.
- “Control reaches end of non-void function”.
- “Variable ‘x’ is used uninitialized”.
6. How to Read Compiler Error Messages
When the compiler prints an error, it usually gives:
- File name (e.g.,
main.c). - Line number (e.g., line 12).
- Column or position.
- A short description of the problem.
Example error message
main.c:12:5: error: expected ';' before 'return'
Interpretation:
- File:
main.c - Line: 12, column: 5
- Problem: the compiler was expecting a semicolon before the keyword
return.
- Always fix errors from top to bottom.
- After fixing a few errors, recompile – don’t wait to fix everything in one shot.
- Read the entire message; sometimes the key hint is at the end.
7. Debugging Tips – Step-by-Step Approach
Debugging is a process, not magic. Here are practical techniques that work in real C programs.
7.1 Use printf as a “software multimeter”
Just like you probe voltages, you can probe variable values using printf:
printf("DEBUG: i = %d, sum = %d\n", i, sum);
Insert such statements around suspicious logic to see:
- Whether loops are running the expected number of times.
- How variables change step by step.
- Where the program “jumps” into unexpected branches.
7.2 Reduce the problem – create a small test program
If a bug appears in a large project:
- Copy the related function / logic into a small separate .c file.
- Hard-code some test data.
- Run only that part until the bug is understood.
7.3 Check boundaries and assumptions
- Array loops: verify start and end index. Are you going from 0 to
n-1or 1 ton? - Division: ensure denominator is never 0.
- Pointers: make sure they are initialized before use; check for
NULL. - File handles: always check if
fopenreturnedNULLbefore reading/writing.
7.4 Use a debugger (gdb / IDE debugger)
Many IDEs (Code::Blocks, VS Code with extensions, etc.) let you:
- Set breakpoints where the program will pause.
- Step through code line by line (
step,next). - Inspect variable values and call stack.
printf-based debugging, gradually practicing with a real debugger
will make you much more efficient.
7.5 Don’t change too many things at once
When debugging:
- Change one small thing, recompile, test.
- If the bug is still there, undo or adjust and try a different change.
- Keep notes of what you tried and what happened.
8. Debugging Checklist – For Every C Program
- I read the first compiler error carefully and fix it before worrying about the rest.
- I treat warnings seriously and try to keep my program warning-free.
- I validate user input (especially for divisions and array sizes).
- I check array bounds and loop limits to avoid off-by-one errors.
- I test formulas with simple known values to catch logical errors early.
- I use
printfor a debugger to trace variable values step by step. - I avoid making many random changes at once; I debug systematically.
- I understand the difference between compile-time, runtime and logical errors.
FAQ: Error Types & Debugging in C
1. What is the main difference between compile-time and runtime errors?
Compile-time errors are detected by the compiler before the program runs, usually due to syntax or type rule violations. The code will not compile until you fix them. Runtime errors occur while the program is executing, even though it compiled successfully. They often involve invalid memory access, divide-by-zero, or file access problems.
2. Why are logical errors harder to find?
Logical errors do not produce compiler errors or obvious crashes. The program appears to work, but it produces incorrect results because the algorithm or formula is wrong. Finding them requires testing with known values, tracing intermediate results and carefully reviewing the logic.
3. Is it okay to ignore compiler warnings if the program runs?
No. Many serious bugs (especially in C) are hinted by warnings: type mismatches, possible uninitialized variables, or suspicious format strings. Aim to reduce warnings to zero for safer and more maintainable code.
4. How can beginners get better at debugging C programs?
Practice a consistent debugging approach: read errors from top to bottom, add temporary
printf statements to inspect variable values, test small pieces of code individually,
and gradually learn to use a debugger (gdb or an IDE debugger). Over time, patterns will become
familiar and you’ll recognize common mistakes quickly.
Buy link ;
Contact link ;
Java Program Structure (Class, main, Methods)– Full Beginner Explanation
Java Program Structure (Class, main, Methods) – Beginner Guide | Tamil Technicians TT Tamil Technicians…
Install JDK + VS Code/IntelliJ Setup+ Your First Java Program (Windows)
Install JDK + VS Code/IntelliJ Setup + First Java Program (Windows) | Tamil Technicians TT…
JDK vs JRE vs JVM – Easy Explanation(Compile & Run Flow for Beginners)
JDK vs JRE vs JVM – Easy Explanation (Beginner Guide) | Tamil Technicians TT Tamil…
Java Introduction & Where Java is Used(Apps, Web, Banking, Android)
Java Introduction & Where Java is Used (Apps, Web, Banking, Android) | Tamil Technicians TT…
Next Step After C – What to Learn? (Embedded C, C++, Python, Linux Programming)
Next Step After C – What to Learn? (Embedded C, C++, Python, Linux Programming) |…
C for Electronics & Technicians – Basic Concepts for Embedded C
C for Electronics & Technicians – Basic Concepts for Embedded C | Tamil Technicians Tamil…
Mini C Project – Student Mark List & Result Generator in C (Console Project)
Mini C Project – Student Mark List & Result Generator in C (Console Project) |…
Mini C Projects – Simple Billing System & Calculator in C (Console Projects)
Mini C Projects – Simple Billing System & Calculator in C (Console Projects) | Tamil…
Error Types in C – Compile Time, Runtime, Logical Errors & Debugging Tips 2025
Error Types in C – Compile Time, Runtime, Logical Errors & Debugging Tips | Tamil…
Preprocessor & Macros in C – #include, #define, Conditional Compilation
Preprocessor & Macros in C – #include, #define, Conditional Compilation | Tamil Technicians Tamil Technicians…
Dynamic Memory Allocation – malloc(), calloc(), realloc(), free() Basics
Dynamic Memory Allocation in C – malloc(), calloc(), realloc(), free() Basics | Tamil Technicians Tamil…
File Handling in C – Read/Write Text Files (Sensor Log, Billing Data Examples) 2025
File Handling in C – Read/Write Text Files (Sensor Log, Billing Data Examples) | Tamil…
Storage Classes in C – auto, static, extern, register (Lifetime & Scope)
Storage Classes in C – auto, static, extern, register (Lifetime & Scope) | Tamil Technicians…
Structures in C – Struct with Examples (Student, Product, Device Data)
Structures in C – Struct with Examples (Student, Product, Device Data) | Tamil Technicians Tamil…
Pointers with Arrays, Strings & Functions – Real Use Cases
Pointers with Arrays, Strings & Functions – Real Use Cases | Tamil Technicians Tamil Technicians…
Pointers in C – Step-by-Step Visual Explanation for Beginners
Pointers in C – Step-by-Step Visual Explanation for Beginners | Tamil Technicians Tamil Technicians –…
Call by Value vs Call by Reference – Simple Pointer Examples in Tamil
Call by Value vs Call by Reference – Simple Pointer Examples in Tamil | Tamil…
Functions in C – User Defined Functions, Arguments, Return Types
Functions in C – User Defined Functions, Arguments, Return Types | Tamil Technicians Tamil Technicians…
Unions & Enums in C – Where and Why to Use Them?
Unions & Enums in C – Where and Why to Use Them? | Tamil Technicians…
Strings in C – char Array, gets(), puts(), String Functions (strlen, strcpy, etc.)
Strings in C – char Array, gets(), puts(), String Functions (strlen, strcpy, etc.) | Tamil…
2D Arrays in C – Tables, Matrices & Small Mini-Projects
2D Arrays in C – Tables, Matrices & Small Mini-Projects | Tamil Technicians Tamil Technicians…
Arrays in C – 1D Array Basics with Practical Examples (Marks, Bills, Readings)
Arrays in C – 1D Array Basics with Practical Examples (Marks, Bills, Readings) | Tamil…
Break, Continue & goto in C – When to Use and When to Avoid?
Break, Continue & goto in C – When to Use and When to Avoid? |…
Loops in C – for, while, do-while Complete Guide (Patterns & Practice)
Loops in C – for, while, do-while Complete Guide (Patterns & Practice) | Tamil Technicians…
switch–case in C – Menu Programs & Real Life Examples for Technicians
switch–case in C – Menu Programs & Real Life Examples for Technicians | Tamil Technicians…
Control Statements in C – if, if-else, nested if, else-if ladder (Tamil Explanation)
Control Statements in C – if, if-else, nested if, else-if ladder (Tamil Explanation) | Tamil…
Operators in C – Arithmetic, Relational, Logical & Assignment (with Examples)
Operators in C – Arithmetic, Relational, Logical & Assignment (with Examples) | Tamil Technicians Tamil…
Input & Output in C – printf(), scanf() Step-by-Step Guide 2025
Input & Output in C – printf(), scanf() Step-by-Step Guide | Tamil Technicians Tamil Technicians…
C Variables, Data Types & Constants Explained in Simple Tamil
C Variables, Data Types & Constants Explained in Simple Tamil | Tamil Technicians Tamil Technicians…
Structure of a C Program – main(), Header Files & How Code Runs?
Structure of a C Program – main(), Header Files & How Code Runs? | Tamil…
What is Programming & Why C is Important? (C Programming in Tamil for Beginners) | Tamil Technicians
What is Programming & Why C is Important? (C Programming in Tamil for Beginners) |…
C Language Introduction & Setup Guide for Beginners 2025
C Language Course Introduction & Setup Guide (Windows, Linux, macOS) | Tamil Technicians Tamil Technicians…
