Understanding programming errors in C

Errors in C are mistakes or the problems that prevent the code from being executed. Everyone can make a mistake unconsciously. Errors are also known as bugs where the process of removing bugs is called debugging. Some errors are detected in time of compilation where some are detected in time of execution of the program.

See this program which has two errors. Try to understand them the given errors in C program.

// program to print the integer from 0 to 10

#include <stdio.h>
int main(){
    int a = 10     // Error: missing semicolon
    
    for (int i = 0; i <= a, i++){     // error: using comma after i <= a
        printf("%d\n", i);
    }

return 0;
}

Though it might be very hard to remove some critical errors but we have to remove them to run the program successfully.

There are several errors in C programming. But we will discuss here only the main five types of errors.

5 main type of errors in C programming

Five main type of errors in C language are as follows.

  1. Syntax error
  2. Run-time error
  3. Linker error
  4. Logical error
  5. Semantic error

Syntax errors in C

Syntax error is generally known as the compilation error because they prevent the program from being compiled. For this, we can also say that the syntax error is thrown by the compilers.

When we make mistakes to write correct syntax of any language, it is called syntax error. Sometimes this occurs due to our typing mistakes. Debugging of these errors are comparatively easy. These types of errors are generally made by beginners.

See the example program of syntax errors in C in the following example.

#include <stdio.h>
int main(){
    int a = 30   // semi colon(;)missed here
    for (int i = 0; i < a; i++){
        printf("%d\n", i);
                    //closing curly brace } missed here
return 0;
}

Run-time errors in C

Compilers can not detect runtime errors in C because runtime error exist during the execution time of a program. After successful compilation we get runtime error. For this the program is running but it is not able to perform the operations. As runtime error can not be detected by compilers, they are very hard to find out. A common example of runtime error is division by zero.

Let’s understand through given program.

#include <stdio.h>
int main(){
    int x = 5;
    int y = x/0;       // cause of runtime error
    printf("%d", y);
    return 0;
}

Linker errors in C

When the executable file is not created then the generated error is called linker error. This can be happening due to the wrong header file in use or wrong function prototyping. The most common linker error which is usually happened is writing Main() instead of main().

Let’s see the program bellow.

#include <stdio.h>
void Main(){  // We should write main() instead of Main()
    int x = 30;
    printf("%d", a);
}

Logical error in C

Logical errors are errors which gives us an undesired output. Actually, they are error free to the compiler but we will get the wrong answer. It is very hard to find out this type of error although it is mostly done by the beginners.

See the two programs bellow which will help you to learn more about logical error in C programming.

#include <stdio.h>
int main(){
    int x = 0;

    for(x = 0; x <= 5; x++);   // unexpected semicolon after loop.
    {
        printf("%d\n", x);
    }
    return 0;
}

In this above program we want to print the number from 0 to 5. But we have not got that on our output because of logical error of a semicolon after the loop.

You will get the correct result if remove the semicolon. Try this.

#include<stdio.h>
int sum(int x, int y);         // method inclusion
void main()                 // main() method
{
int x = 20;
int y = 30;

printf("Sum of %d and %d is = %d\n", x, y, sum(x, y));
}

int sum(int x, int y)
{
return x * y;      //instead of sum 50 we will get 600 here
}                 //because we have given * instead of + operator.

Semantic errors in C

Semantic errors are the error thrown by the compiler if the code is not meaningful to the compiler to compile the program. The compiler will give this type of error if the program is not understandable to the compiler.

Here we have given an example of semantic error.

#include <stdio.h>
int main(){
int x = 10;
int y = 30;
int z = 40;

x + y = z;       // semantic error
printf("%d\n", z);
return 0;
}