Error may be the most common word that irritates most of us.So, lets see some of the most common errors in programming and some tips to fix them.
errors are also known as bugs that causes the program to either run unexpectedly (shows unexpected result) or prevent the execution of a program.
Syntax errors represent grammar errors in the use of the programming language. These usually occur at compile-time.
Syntax errors are the easiest to find and fix. Over the years, compiler developers have worked hard to make compilers smarter so that they can catch errors at compile time that might otherwise turn out to be run time errors.
These are valid code the compiler understands, but they do not what you, the programmer, intended. These may be using the wrong variable, the wrong operation, or operations in the wrong order. There is no way for the compiler to detect them.
As the name itself implies, these errors are related to the logic of the program. Logical errors are also not detected by compiler and cause incorrect results.
These errors occur due to incorrect translation of algorithm into the program or occurs due to poor understanding of the logic
Never use any relational operator while dealing with floating point integers.
Linker errors usually occur when there are certain necessary files that are not linked for successful execution of the program.These are mostly occur when we do not include header files for the predefined functions used in a program and when we misspell a standard c function.
However,some compilers add certain necessary header files by themself that do not cause error
Check whether the necessary files are included in the program or not.
Latent Errors are the hidden errors that occur only when a particular set of input is given.
result = (a+b)/(c-d);
Here,an error occurs only when c and d are equal because that will make remainder zero (divide by zero error).
Be clear,about the logic of the program and consider all possible combinations of input data to detect the errors.
These are not detected by compiler while compilation process. A program with these kinds of errors will run but produce erroneous results or may cause abnormal termination of program. Detection and removal of a run-time error is a difficult task.
what is an 'error':
errors are also known as bugs that causes the program to either run unexpectedly (shows unexpected result) or prevent the execution of a program.
Types Of Errors:
- Syntax Errors (Compiler errors or Compile-time errors)
- Semantic Errors.
- Logical Errors.
- Linker Errors.
- Latent errors.
- Runtime Errors.
SYNTAX ERRORS
Syntax errors represent grammar errors in the use of the programming language. These usually occur at compile-time.
Some of the examples of the Compiler errors are:
- Misspelled variable ,function names and Missing semicolons
- Unmatched parentheses, square brackets, and curly braces
- Using a variable that has not been declared
- Incorrect format in selection and loop statements
Example
void main() { int a=10 // Syntax error as semicolon is missing int b=100 : // Syntax error as using ':' instead of ';' c=a+b; //Syntax error as c is undeclared } } //Syntax error for unbalanced parenthesis
TIPS TO FIX :-
Syntax errors are the easiest to find and fix. Over the years, compiler developers have worked hard to make compilers smarter so that they can catch errors at compile time that might otherwise turn out to be run time errors.
Semantic Errors
These are valid code the compiler understands, but they do not what you, the programmer, intended. These may be using the wrong variable, the wrong operation, or operations in the wrong order. There is no way for the compiler to detect them.
Example :
void main() { int a+b=c; // Semantic Error int c=a+b; //correct one int a=+b; // Semantic Error int a+=b; //correct one }
Logical Errors:
As the name itself implies, these errors are related to the logic of the program. Logical errors are also not detected by compiler and cause incorrect results.
Why do we get Logical errors:
These errors occur due to incorrect translation of algorithm into the program or occurs due to poor understanding of the logic
Logic errors are the hardest to find and fix because:
- These errors cannot be found by the compilers.
- Program executes normally,but results in unpredictable outputs.
- Program may give correct results for only certain set of inputs.
Examples:
void main() { float a,b; printf("enter the values of a and b :- "); scanf("%f %f",&a,&b); if(a==b) // When a and b are float types values, they rarely become equal due to truncation errors. printf("these two are equal"); }
TIPS TO FIX :
Never use any relational operator while dealing with floating point integers.
Linker Errors:
Linker errors usually occur when there are certain necessary files that are not linked for successful execution of the program.These are mostly occur when we do not include header files for the predefined functions used in a program and when we misspell a standard c function.
Example 1:
void mian() // Linker error as 'main' is misspelled as 'mian' { printf("welcome to code titans "); }
Example 2:
#include"stdio.h" void main() { int x,res; printf("enter the value of x"); scanf("%d",&x); res=pow(x,2); //linker error as pow is declared without using header file math.h printf("square of x is %d",res); }
However,some compilers add certain necessary header files by themself that do not cause error
TIPS TO FIX:
Check whether the necessary files are included in the program or not.
Latent errors :
Latent Errors are the hidden errors that occur only when a particular set of input is given.
Example:
result = (a+b)/(c-d);
Here,an error occurs only when c and d are equal because that will make remainder zero (divide by zero error).
TIPS TO FIX:
Be clear,about the logic of the program and consider all possible combinations of input data to detect the errors.
Runtime Errors:
Runtime errors are the errors that occur during the execution of the program.Some examples are:
- Dividing any number by zero .
- Insufficient memory for dynamic memory allocation.
- Referencing an out-of-range array element.etc
These are not detected by compiler while compilation process. A program with these kinds of errors will run but produce erroneous results or may cause abnormal termination of program. Detection and removal of a run-time error is a difficult task.
Examples of some illegal operations that may produce runtime errors are :
- Dividing a number by zero
- Trying to open a file which is not created
- Lack of free memory space
Example :
void main() { int a=10,b=0,result; int number; result=a/b; // Runtime error scanf("%d",&number); }
No comments:
Post a Comment