Skip to content
StudyFame

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.

TypeSimple meaningFound byWhen
Syntax errorBreaking the grammar rules of CCompilerCompile time
Semantic errorGrammar is fine, but the statement makes no senseCompilerCompile time
Linker errorA function is used but cannot be foundLinkerCompile time (linking)
Runtime errorThe program crashes while runningThe system, while runningRun time
Logical errorThe program runs but gives the wrong answerOnly 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

  1. int a = 2, b = 3;: two whole numbers.
  2. (a + b) / 2: a + b is 5, and 5 / 2 with two integers gives 2. Integer division throws away the decimal part.
  3. Only after that, 2 is stored in the float as 2.00. The 0.5 is already lost.
  4. The compiler gives no error, because the code is valid C. This is a logical error.

How to Find and Fix Errors

  1. Read the first error message first. One mistake can cause many messages below it.
  2. Look at the file name and line number, e.g. sum.c:6:5 means file sum.c, line 6, column 5.
  3. Check the line above too. A missing ; is usually reported on the next line.
  4. For logical errors, test with values you know the answer to, and print variables to see where the value goes wrong.
  5. Compile with -Wall so 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

MistakeBetter way
Trying to fix all 20 error messages at onceFix the first one and compile again. Many others disappear.
Looking only at the reported lineAlso check the line above (missing ; or }).
Thinking "no error" means "correct"Test the output. It may still have a logical error.
Ignoring warningsWarnings often point to real bugs. Fix them too.

Errors in C Practice Questions

  1. Name the 5 types of errors in C.
  2. What is the difference between a syntax error and a logical error?
  3. Which errors are found at compile time?
  4. Give one example of a runtime error.
  5. Why is a logical error the hardest to find?
Show answers
  1. Syntax, semantic, linker, runtime and logical errors.
  2. A syntax error breaks the grammar rules and stops compilation. A logical error compiles and runs, but gives the wrong result.
  3. Syntax, semantic and linker errors.
  4. Dividing an integer by zero (also: using an invalid memory address).
  5. 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
  1. Missing ; after b = 20 (syntax error).
  2. a - b should be a + b (logical error).
  3. Printf should be printf (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)

  1. A missing semicolon is a:

    A) Logical error   B) Syntax error   C) Runtime error   D) Linker error

  2. A program runs but prints the wrong answer. This is a:

    A) Syntax error   B) Linker error   C) Logical error   D) Semantic error

  3. undefined reference to `main' is a:

    A) Linker error   B) Runtime error   C) Logical error   D) Warning

  4. Integer division by zero while running is a:

    A) Syntax error   B) Runtime error   C) Semantic error   D) Linker error

  5. What does (5) / 2 give in C when both are integers?

    A) 2.5   B) 3   C) 2   D) Error

Show answers
  1. B: syntax error.
  2. C: logical error.
  3. A: linker error.
  4. B: runtime error.
  5. 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 typesSyntax, Semantic, Linker, Runtime, Logical
Compile-time errorsSyntax, Semantic, Linker
Run-time errorsRuntime, Logical
Syntax error exampleMissing ;
Semantic error examplea + b = c;
Linker error exampleMain() instead of main()
Runtime error exampleDivide 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.