There are mainly five types of errors exist in C programming:
- Syntax error
- Run-time error
- Linker error
- Logical error
- Semantic error
Syntax error
Syntax errors, often referred to as compilation errors, arise during compilation or are specifically thrown by compilers. They typically stem from typos or deviations from the syntax rules of the programming language in use. Novice programmers commonly make these mistakes due to their unfamiliarity with the language. Fortunately, these errors are relatively easy to debug and rectify.
For example:
If we want to declare the variable of type integer,
int a; // this is the correct form
Int a; // this is an incorrect form.
Commonly occurred syntax errors are:
- If we miss the parenthesis (}) while writing the code.
- Displaying the value of a variable without its declaration.
- If we miss the semicolon (;) at the end of the statement.
Let's understand through an example.
#include <stdio.h>
int main()
{
a = 10;
printf("The value of a is : %d", a);
return 0;
}
Output
[Program Output]
In the previously mentioned output, we can see that the code encounters an issue where 'a' is referenced without being declared, resulting in a syntax error.
There is an alternative scenario where syntax errors may arise, specifically when errors occur in the fundamental structure. Let's explore this situation using an illustration.
#include <stdio.h>
int main()
{
int a=2;
if(.) // syntax error
printf("a is greater than 1");
return 0;
}
In the code snippet above, a period (.) was mistakenly used instead of a condition within the 'if' statement. This error results in a syntax issue, as depicted in the screenshot below.
Output
[Program Output]
Run-time error
Sometimes, errors can occur during the execution phase even after the compilation is successful, which are referred to as run-time errors. When a program is in operation but encounters issues that prevent it from completing an operation, it results in a run-time error. An example of a common run-time error is division by zero. Identifying these errors can be challenging as the compiler does not detect them during the compilation process.
Let's understand through an example.
#include <stdio.h>
int main()
{
int a=2;
int b=2/0;
printf("The value of b is : %d", b);
return 0;
}
Output
[Program Output]
In the aforementioned result, we can see that the program encounters a run-time issue, specifically a division by zero error.
Linker error
Linker issues typically arise when the program's executable file is not properly generated. This situation can occur due to inaccuracies in function prototyping or the inclusion of incorrect header files. For instance, consider a scenario where the main.c file includes a sub function with its declaration and definition located in another file like func.c. When compiling, the compiler identifies the sub function in func.c, resulting in the creation of two object files: main.o and func.o. If, at runtime, the definition of the sub function is missing in func.o, a linker error will be triggered. One common linker error involves mistakenly using Main instead of main.
Let's understand through a simple example.
#include <stdio.h>
int Main()
{
int a=78;
printf("The value of a is : %d", a);
return 0;
}
Output
[Program Output]
Logical error
A logical error results in an unexpected outcome, generating inaccurate results while technically free from errors. Typically associated with novice programmers, these mistakes stem from the developer's logical reasoning. A strong logical foundation reduces the likelihood of encountering such errors in code.
Let's understand through an example.
#include <stdio.h>
int main()
{
int sum=0; // variable initialization
int k=1;
for(int i=1;i<=10;i++); // logical error, as we put the semicolon after loop
{
sum=sum+k;
k++;
}
printf("The value of sum is %d", sum);
return 0;
}
Output
[Program Output]
In the aforementioned code snippet, our intention was to display the total of 10 digits. However, an error occurred where a semicolon (;) was mistakenly placed after the for loop. Consequently, the statements within the for loop did not execute properly, resulting in an incorrect output.
Semantic error
Semantic errors are mistakes that occur when the compiler encounters statements that are not interpretable.
The following can be the cases for the semantic error:
- Use of a un-initialized variable. int i; i=i+2;
- Type compatibility int b = "logic practice";
- Errors in expressions int a, b, c; a+b = c;
- Array index out of bound int a[10]; a[10] = 34;
Let's understand through an example.
#include <stdio.h>
int main()
{
int a,b,c;
a=2;
b=3;
c=1;
a+b=c; // semantic error
return 0;
}
In the provided code snippet, the expression a+b =c is used, which is invalid because operands cannot be placed on the left side of an assignment operator.
Output