Errors in C
Errors in C
Errors are nothing but problems or faults that pretty much occur in all the programming languages. Errors make the behavior of the program seem abnormal, and even the experienced programmer makes one. Programming errors are also referred to as bugs or faults, and removing these errors is termed debugging.
It is an illegal operation that a developer performs, which leads to the abnormal working conditions of that program. Errors remain unnoticed until and unless the compiler compiles or is executed by the runtime. Many errors exhibit the program from getting compiled and executed. Hence, they have to debug for smooth compiling and executing.
Errors lead to the abrupt and illegal operation of the logic of the program. Many errors in the C programming language are hidden and prevented from the program from compilation or execution. Hence, while executing any application, for that matter, one should successfully take out the errors present within the program.
Consider an instance where a program has been written to display the product of two numbers. But while defining the variables, a programmer forgets to put the semicolon at the end, which results in an error while compiling. Hence, they are either identified during compilation or execution.
Types of Error in C
There are five types of errors that exist in the C standard:
Syntax error
- These types of errors when the program or an application violates the rule of the programming standard.
- Syntax errors also known as the compilation errors as they occur at the compilation time or the syntax errors since they are thrown by the compilers.
- These are the errors that mainly occur due to the mistakes while typing down the code or not following the syntax according to the C standard.
- They are usually done by the beginners as they may be new to the programming language.
- They can be debugged easily and rewritten.
E.g.:
int x; Int x;
The above-mentioned syntax is different from each other and the latter gives the syntax error due to case sensitivity.
Common errors in these types include;
- Missing semicolon at the end of the code.
- Displaying value without any declaration.
- Missing parentheses.
- Case sensitivity.
#include <stdio.h> #include <conio.h> int main() { x = 10; printf (“The value of a is: %d”, a); return 0; }
Output:
Runtime error
- Errors that occur at the time of execution of a program or a runtime after successful compilation are referred to as runtime errors.
- One of the most common runtime errors occur when a number is divided by zero also known as division error.
- These are the types of errors which are pretty hard to guess as the compiler does not point where the actual error occurs.
E.g.:
#include<stdio.h> void main() { int n = 9, div = 0; div = n/0; printf("resut = %d", div); }
Output:
Linker error
- This type of errors is occurred when the program is compiled successfully, and trying to link the different objects file with the main object file.
- Whenever this error is occurred, the executable is not generated such as wrong function prototyping, incorrect header files, and so on.
- Whenever the main() function has case sensitivity issues, this error arouses.
E.g.:
#include<stdio.h> main() { int x = 52; int y = 0; printf("Div : %f", x/y); }
Output:
Logical error
- A logical error is one of the types of error in the C language that arouses an undesired output.
- They are error free but still give errors which are logical.
- They occur when the errors depend upon the logical thinking of the developer.
- If the programmers sound logically good, then there might be a fewer cases of these errors.
E.g.:
#include<stdio.h> #include<conio.h> int main() { int sum = 0; int k = 1; for (int i = 1; i <= 10; i++); { sum = sum +k; k++; } printf (“The value of the sum is %d”, sum); return 0; }
Output:
In the above code snippet, a for loop has a semicolon hence the inner loop will not execute and produces the output as 1.
Semantic error
- It is the type of error that occurs when it syntactically is correct but have no meaning.
- If an expression is given at the left side of the assignment operator, then it generates a semantic error.
E.g.:
#include<stdio.h> int main() { int a, b, c; a = 10; b = 20; a + b = c; }
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 = "tutorials and examples";
- Errors in expressions
int a, b, c;
a+b = c;
- Array index out of bound
int a[10];
a[10] = 34;