Table of contents
1.
Introduction
2.
Syntax Errors
3.
Runtime Errors
4.
Logical Errors
5.
Linked Errors
6.
Semantic Errors
7.
Frequently Asked Questions
7.1.
What is the most common type of error in C programming?
7.2.
How can I debug runtime errors in my C program?
7.3.
What are some best practices to avoid logical errors in C programming?
8.
Conclusion
Last Updated: Nov 24, 2024
Easy

Types of Errors in C

Author Rahul Singh
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Errors are an inevitable part of programming. Every programmer might have encountered them at almost every stage of coding. They occur when something goes wrong in the code and prevents it from running as intended. In the C programming language, programmers may also encounter several types of errors. Knowledge of why this error is occurring or what type of error this is crucial for debugging and writing efficient code. 

Types of Errors in C

In this article, we will discuss the different types of errors in C, like syntax errors, runtime errors, logical errors, linked errors, and semantic errors, with relevant examples of each error type and discuss how to identify and resolve them. 

Syntax Errors

Syntax errors occur when code violates the rules of the C programming language. The compiler detects these errors during the compilation process and prevents the code from being compiled successfully. Syntax errors can include missing semicolons, incorrect use of brackets, or misspelled keywords. 

For example: 

#include <stdio.h>

int main() {
    printf("Hello, World!")
    return 0;
}
You can also try this code with Online C Compiler
Run Code


Output

ERROR!
/tmp/Naw05BoyzF.c: In function 'main':
/tmp/Naw05BoyzF.c:4:28: error: expected ';' before 'return'
   4 |     printf("Hello, World!")
     |                            ^
     |                            ;
   5 |     return 0;
     |     ~~~~~~                  

=== Code Exited With Errors ===


This code lacks a semicolon after the `print` statement. When attempting to compile it, the compiler throws a syntax error, indicating that a semicolon is expected.

To fix syntax errors, carefully review the code and ensure that all statements are properly terminated with semicolons, brackets are balanced, and keywords are spelled correctly. Modern integrated development environments (IDEs) often provide helpful error messages and highlighting to assist in identifying syntax errors.

Runtime Errors

Runtime errors, also known as execution errors, occur during a program's execution. They happen when the program tries to perform an operation that is not allowed or encounters an unexpected condition. Runtime errors can lead to program crashes or unexpected behavior.

One common example of a runtime error is division by zero: 

#include <stdio.h>

int main() {
    int num1 = 10;
    int num2 = 0;
    int result = num1 / num2;
    printf("Result: %d\n", result);
    return 0;
}
You can also try this code with Online C Compiler
Run Code


Output

Floating point exception


In this code, we attempt to divide `num1` by `num2`. However, `num2` is set to zero, which is not allowed in division. When the program reaches the line `int result = num1 / num2;`, it will encounter a runtime error, specifically a division by zero error. The program will terminate abruptly.

To avoid runtime errors, it is important to handle potential error conditions in the code. In the case of division, we can add a check to ensure that the divisor is not zero before performing the division:

#include <stdio.h>

int main() {
    int num1 = 10;
    int num2 = 0;
    if (num2 != 0) {
        int result = num1 / num2;
        printf("Result: %d\n", result);
    } else {
        printf("Error: Division by zero is not allowed.\n");
    }
    return 0;
}
You can also try this code with Online C Compiler
Run Code

 

Output

ERROR!
Error: Division by zero is not allowed.


By adding a conditional statement to check if `num2` is not equal to zero, we can prevent the division by zero error & display an appropriate error message instead.

Logical Errors

Logical errors occur when a program runs without any syntax or runtime errors but produces incorrect or unintended results. These errors are caused by flaws in the program's logic or algorithm. Logical errors can be challenging to identify and debug because the program may appear to be running correctly.

Let's take an example of a logical error in a program that calculates the average of three numbers:

#include <stdio.h>
int main() {
    int num1 = 10;
    int num2 = 20;
    int num3 = 30;
    int sum = num1 + num2 + num3;
    int average = sum / 2;
    printf("Average: %d\n", average);
    return 0;
}
You can also try this code with Online C Compiler
Run Code

 

Output

Average: 30


This code intends to calculate the average of three numbers. However, there is a logical error in the calculation. The code divides the sum by 2 instead of 3, resulting in an incorrect average.

To fix logical errors, it is necessary to carefully review the program's logic & ensure that the algorithm is correct. In this case, we need to divide the sum by 3 to obtain the correct average:

#include <stdio.h>
int main() {
    int num1 = 10;
    int num2 = 20;
    int num3 = 30;
    int sum = num1 + num2 + num3;
    int average = sum / 3;
    printf("Average: %d\n", average);
    return 0;
}
You can also try this code with Online C Compiler
Run Code

 

Output

Average: 20


By dividing the sum by 3, we obtain the correct average of the three numbers.

Logical errors can be more subtle and harder to identify than syntax and runtime errors. Thorough testing, including edge cases and different input scenarios, can help uncover logical errors in the program.

Linked Errors

Linked errors occur when there are issues with a program's linking process. Linking is the process of combining object files and libraries to create an executable program. Linked errors happen when there are missing or incompatible libraries, undefined symbols, or conflicts between different parts of the program.

For example: 

Suppose we have two C source files, `main.c` and `helper.c`, and a header file `helper.h`. The `main.c` file includes the `helper.h` header and calls a function defined in `helper.c`. However, during the linking process, if the `helper.c` file is not compiled or the resulting object file is not linked properly, a linked error will occur.

// main.c
#include <stdio.h>
#include "helper.h"

int main() {
    int result = multiply(5, 3);
    printf("Result: %d\n", result);
    return 0;
}

// helper.h
#ifndef HELPER_H
#define HELPER_H


int multiply(int a, int b);
#endif
// helper.c
int multiply(int a, int b) {
    return a * b;
}
You can also try this code with Online C Compiler
Run Code

 

Output

ERROR!
/tmp/tFmOiMKpve.c:3:10: fatal error: helper.h: No such file or directory
   3 | #include "helper.h"
     |          ^~~~~~~~~~
compilation terminated.


If the `helper.c` file is not compiled or linked properly, the linker will throw an error indicating that the `multiply` function is undefined.

To resolve linked errors, ensure that all necessary object files and libraries are included in the linking process. Double-check that the function declarations in header files match the actual function definitions in the corresponding source files. Additionally, make sure that the object files are up to date and rebuilt if any changes have been made to the source files.

Linked errors can also occur due to naming conflicts or the use of incompatible libraries. It's important to use unique names for functions, variables, and symbols to avoid conflicts and ensure that the correct versions of libraries are being used.

Semantic Errors

 Semantic errors occur when the program compiles and runs without any syntax, runtime, or linked errors, but it does not behave as intended or produces incorrect results. Semantic errors are related to the program's meaning or logic and can be caused by misunderstanding the problem, using incorrect algorithms, or making incorrect assumptions.

For example: 

#include <stdio.h>

int factorial(int n) {
    int result = 1;
    for (int i = 1; i <= n; i++) {
        result *= i;
    }
    return result;
}

int main() {
    int num = 5;
    int fact = factorial(num);
    printf("Factorial of %d is: %d\n", num, fact);
    return 0;
}
You can also try this code with Online C Compiler
Run Code

 

Output

Factorial of 5 is: 120


In this code, the `factorial` function calculates the factorial of a given number. However, there is a semantic error in the function. The factorial of 0 is defined as 1, but the current implementation will return an incorrect result for `factorial(0)` because the loop condition `i <= n` is not satisfied when `n` is 0.

To fix this semantic error, we need to modify the `factorial` function to handle the case when `n` is 0 correctly:

int factorial(int n) {
    if (n == 0) {
        return 1;
    }
    int result = 1;
    for (int i = 1; i <= n; i++) {
        result *= i;
    }
    return result;
}


By adding a special case for `n == 0` & returning 1 directly, we ensure that the factorial of 0 is calculated correctly.

Semantic errors can be subtle and may require a deep understanding of the problem domain and the program's expected behavior. Thorough testing with various inputs, including edge cases, can help identify semantic errors. Code reviews and discussions with other programmers can also be beneficial in catching semantic errors.

Frequently Asked Questions

What is the most common type of error in C programming?

Syntax errors are the most common type of error in C programming. They occur when the code violates the rules of the C language, such as missing semicolons or incorrect use of brackets.

How can I debug runtime errors in my C program?

To debug runtime errors, you can use a debugger tool to step through your code line by line, examine variable values, and identify the exact location where the error occurs. Adding print statements can also help track the program flow and narrow down the issue.

What are some best practices to avoid logical errors in C programming?

To avoid logical errors, it's important to have a clear understanding of the problem and the expected behavior of the program. Break down the problem into smaller subproblems, design a logical algorithm, and test thoroughly with various inputs, including edge cases. Code reviews and discussions with peers can also help identify logical errors.

Conclusion

In this article, we discussed the different types of errors in C programming, like syntax errors, runtime errors, logical errors, linked errors, and semantic errors. We provided examples of each error type and discussed strategies to identify and resolve them. 

You can also check out our other blogs on Code360.

Live masterclass