C-Language
Errors
❑ Error is an illegal operation performed by the user which results in
abnormal working of the program.
❑ Programming errors remain undetected until the program is compiled or
executed. These errors are also referred to as program bugs.
❑ Some of the errors inhibit the program from getting compiled or executed.
❑ Usually, the errors become more difficult to find and fix.
Syntax
❑ Errors
The set of rules (grammatical rules) for writingstatements of the computer
program is known as syntax of the language. The program statements are
written strictly according to these rules.
❑ Syntax error occur when syntax of a programming language are not followed
in writing the source code. The compiler detects these errors & reports a
proper error message at compiling time of source code.
❑ The syntax errors are easy to detect and remove.
❑ Also known as Compile-time errors.
❑ Missing semicolon ( ; ) at the end of statement
❑ Missing Parenthesis (})
❑ Printing the value of variable without declaring it
❑ Incorrect spelling of any keyword
Syntax
Errors
// C program to illustrate
// syntax error
#include<stdio.h>
void main()
{
int x = 10;
int y = 15;
printf("%d", (x, y)) // semicolon missed
}
Logical Errors
❑ The errors in the logic of the program are called logical error.
❑ The compiler cannot detect logical errors. A program with logical errors is
compiled and run successfully but it does not give correct result.
❑ There is no indication of error when the program is executed thus hardest to
find and fix such errors.
#include<stdio.h> Here we want the line will be printed five
main() times. But only one time it will be
{ printed for the block of code
int i; for(i = 0; i<5; i++);
{ The program may produce correct
printf("Hello World"); results for some input data and wrong
} results for other input data
}
Logical Errors
#include <stdio.h>
#include
<stdlib.h> int
main(void) { int
num1, num2;
printf("Enter the first integer:
"); scanf("%d", &num1);
printf("\nEnter the second
integer: "); scanf("%d", &num2);
printf("\n%d + %d %d\n", num1, num2, num1 *
= num2);
system("PAUSE");
return 0;
}
Logical Errors
Notice that the program produces correct results for some inputs:
num1 = 2, num2 = 2
and
num1 = 0, num2 = 0 but wrong results for others.
Runtime Errors
❑ The errors that occur during the execution of program after successful
compilation are called the runtime errors. These types of errors may occur
due to the following reasons:
❑ When the program attempts to perform an illegal operation such as
dividing a number by zero.
❑ If input data given to the program is not in a correct format or input
data file is not found in the specified path.
❑ If hardware problem occurs such as hard disk error, or disk full or
printer error etc.
❑ When a runtime error occurs, the computer stops the execution of program
and displays an error message.
❑ To find the source of a run-time error in a program, usually a software called
debugger is used.
Runtime Errors
When the following program is executed and the user inputs 0 for num2, a
run-time error occurs due to division by zero in the expression num1 / num2
#include <stdio.h>
#include <stdlib.h>
int main(void) {
int num1, num2;
printf("Enter the first integer: ");
scanf("%d", &num1);
printf("\nEnter the second integer: ");
scanf("%d", &num2);
printf("\n%d + %d = %d", num1, num2,
+ num2); num1
printf("\n%d / %d = %d", num1, num2,
num1
/ num2);
printf("\n%d * %d
= %d\n", num1, num2,
num1 * num2);
system("PAUSE");
return 0;
}
Linker errors
❑ These error occurs when after compilation compiler tries to link the
different object files with main’s Object Code.
❑ These are errors generated when the executable code of the program
cannot be generated.
❑ Usually occurs due to:
❑ When the linker encounters what looks like a function call but it
cannot find a function with that name
❑ Incorrect header file
❑ Not including the header file for a function
Linker
errors
Not including the header file for a function sqrt
Linker
errors
Not including a standard header file in a program will generate a linker error
Semantic errors
❑ This error occurs when the statements written in the program are not
meaningful to the compiler.
❑ A semantic error occurs when a statement is syntactically valid, but does not
do what the programmer intended.
❑ Semantic errors are problems with a program that runs without
producing error messages but doesn't do the right thing.
❑ Identifying semantic errors can be tricky because it requires a programmer to
work backward by looking at the output of the program and trying to figure
out what it is doing.
❑ Modern compilers have been getting better at detecting certain types of
common semantic errors.
void main()
{ int a, b, c;
a + b = c; //semantic error }