Types of Errors in C Programming
Every programmer gets errors, every day. The skill is not "never making errors". The skill is knowing what kind of error it is, so you can fix it fast.
In this lesson you will learn the 5 types of errors in C, see each one in a small program with its real error message, and learn how to fix it. The messages shown are from GCC; other compilers use slightly different words for the same errors.
What You Will Learn
- What an error (bug) is.
- The 5 types of errors: syntax, semantic, linker, runtime and logical.
- The difference between compile-time and runtime errors.
- How to read an error message.
- The difference between an error and a warning.
Types of Errors in C
Exam definition: An error is a mistake in a program that stops it from compiling, running or giving the correct result. C has five types of errors: syntax, semantic, linker, runtime and logical.
| Type | Simple meaning | Found by | When |
|---|---|---|---|
| Syntax error | Breaking the grammar rules of C | Compiler | Compile time |
| Semantic error | Grammar is fine, but the statement makes no sense | Compiler | Compile time |
| Linker error | A function is used but cannot be found | Linker | Compile time (linking) |
| Runtime error | The program crashes while running | The system, while running | Run time |
| Logical error | The program runs but gives the wrong answer | Only you (by testing) | Run time |
Memory trick: "Some Students Love Running Late" = Syntax, Semantic, Linker, Runtime, Logical. The first three are caught before running; the last two happen while running.
1. Syntax error
Like a spelling or grammar mistake in English. The most common one: a missing semicolon.
printf("Hello")
return 0;
error: expected ';' before 'return'
2. Semantic error
The words are correct, but the meaning is wrong. You cannot store a value into a + b.
int a = 5, b = 10, c;
a + b = c;
error: lvalue required as left operand of assignment
3. Linker error
The code compiles, but the linker cannot find a function. Here main is spelled with a capital M:
int Main(void)
{
return 0;
}
undefined reference to `main'
On Windows (MinGW) the message may say undefined reference to `WinMain'. It is the same problem.
4. Runtime error
The program compiles fine, but crashes while running. Dividing an integer by zero is a classic example:
int x = 10, y = 0;
printf("%d\n", x / y);
Floating point exception (core dumped)
This message is from Linux. On Windows the program may just stop without printing anything.
5. Logical error
The most dangerous one: no message at all, the program runs, but the answer is wrong. See Example 1 below.
Error vs warning
- An error stops the program from being created.
- A warning is a hint. The program is still created, but something may be wrong. Never ignore warnings while learning.
Example 1: A Logical Error (Wrong Average)
This program should print the average of 2 and 3, which is 2.5. Read the output carefully:
#include <stdio.h>
int main(void)
{
int a = 2, b = 3;
float average = (a + b) / 2;
printf("Average = %.2f\n", average);
return 0;
}
Output
Average = 2.00
Code Explanation
int a = 2, b = 3;: two whole numbers.(a + b) / 2:a + bis 5, and 5 / 2 with two integers gives 2. Integer division throws away the decimal part.- Only after that, 2 is stored in the
floatas 2.00. The 0.5 is already lost. - The compiler gives no error, because the code is valid C. This is a logical error.
How to Find and Fix Errors
- Read the first error message first. One mistake can cause many messages below it.
- Look at the file name and line number, e.g.
sum.c:6:5means filesum.c, line 6, column 5. - Check the line above too. A missing
;is usually reported on the next line. - For logical errors, test with values you know the answer to, and print variables to see where the value goes wrong.
- Compile with
-Wallso the compiler warns you about suspicious code.
Example 2: Fixing the Logical Error
Divide by 2.0 instead of 2. Now one side is a decimal number, so C does decimal division:
#include <stdio.h>
int main(void)
{
int a = 2, b = 3;
float average = (a + b) / 2.0f;
printf("Average = %.2f\n", average);
return 0;
}
Output
Average = 2.50
Important Points to Remember
- 5 types: syntax, semantic, linker, runtime, logical.
- Compile-time errors: syntax, semantic and linker. The program is not created.
- Runtime errors: the program crashes while running.
- Logical errors: no message, wrong output. Hardest to find.
- Always fix the first error message first.
Common Mistakes When Fixing Errors
| Mistake | Better way |
|---|---|
| Trying to fix all 20 error messages at once | Fix the first one and compile again. Many others disappear. |
| Looking only at the reported line | Also check the line above (missing ; or }). |
| Thinking "no error" means "correct" | Test the output. It may still have a logical error. |
| Ignoring warnings | Warnings often point to real bugs. Fix them too. |
Errors in C Practice Questions
- Name the 5 types of errors in C.
- What is the difference between a syntax error and a logical error?
- Which errors are found at compile time?
- Give one example of a runtime error.
- Why is a logical error the hardest to find?
Show answers
- Syntax, semantic, linker, runtime and logical errors.
- A syntax error breaks the grammar rules and stops compilation. A logical error compiles and runs, but gives the wrong result.
- Syntax, semantic and linker errors.
- Dividing an integer by zero (also: using an invalid memory address).
- Because there is no error message; the program runs and only the output is wrong.
Coding Exercise
This program has 3 errors. Find and fix them all. It should print Sum = 30.
#include <stdio.h>
int main(void)
{
int a = 10, b = 20
int sum = a - b;
Printf("Sum = %d\n", sum);
return 0;
}
Show solution
- Missing
;afterb = 20(syntax error). a - bshould bea + b(logical error).Printfshould beprintf(C is case-sensitive; it gives a compile or linker error).
#include <stdio.h>
int main(void)
{
int a = 10, b = 20;
int sum = a + b;
printf("Sum = %d\n", sum);
return 0;
}
Sum = 30
Quick Quiz: Errors in C (MCQ)
-
A missing semicolon is a:
A) Logical error B) Syntax error C) Runtime error D) Linker error
-
A program runs but prints the wrong answer. This is a:
A) Syntax error B) Linker error C) Logical error D) Semantic error
-
undefined reference to `main'is a:A) Linker error B) Runtime error C) Logical error D) Warning
-
Integer division by zero while running is a:
A) Syntax error B) Runtime error C) Semantic error D) Linker error
-
What does
(5) / 2give in C when both are integers?A) 2.5 B) 3 C) 2 D) Error
Show answers
- B: syntax error.
- C: logical error.
- A: linker error.
- B: runtime error.
- C: 2. Integer division drops the decimal part.
Quick Revision for Exams and Interviews
Read this before your exam. The highlighted words are the answer.
Must-remember facts
| 5 types | Syntax, Semantic, Linker, Runtime, Logical |
|---|---|
| Compile-time errors | Syntax, Semantic, Linker |
| Run-time errors | Runtime, Logical |
| Syntax error example | Missing ; |
| Semantic error example | a + b = c; |
| Linker error example | Main() instead of main() |
| Runtime error example | Divide by zero |
| Logical error example | (2 + 3) / 2 gives 2, not 2.5 |
Frequently asked questions
- What is the difference between compile-time and runtime errors?
- Compile-time errors stop the program from being created; runtime errors happen while the program is running.
- Which error is the hardest to find?
- A logical error, because there is no error message, only a wrong answer.
- What is a semantic error?
- The grammar is correct but the statement has no valid meaning, like
a + b = c;. - What causes a linker error?
- A function is used but its definition cannot be found, like a misspelled
main. - Is a warning an error?
- No. The program is still created, but a warning points to a possible bug.
Remember
- Order trick: S-S-L-R-L ("Some Students Love Running Late").
- Fix the first error first, then compile again.
Next Topic
Next lesson: Comments in C Programming. You will learn how to write notes inside your code that the compiler ignores.