Vous êtes sur la page 1sur 22

KLE Society’s

B. V. Bhoomaraddi. College of
Engineering and Technology

Course Title: Programming in C.


Code: 15ECSP101
Introduction to Debugging
KLE Society’s
B. V. Bhoomaraddi. College of
Engineering and Technology

Contents

3/11/2018 K. L. E. Technological University, Hubli- 2


31.
KLE Society’s
B. V. Bhoomaraddi. College of
Engineering and Technology

3/11/2018 K.L.E Technological University, Hubballi-31


3
KLE Society’s
B. V. Bhoomaraddi. College of
Engineering and Technology

TYPES OF ERRORS/BUGS

3/11/2018 K. L. E. Technological University, Hubli- 4


31.
KLE Society’s
B. V. Bhoomaraddi. College of
Engineering and Technology

Types of Bugs/Errors
Syntax Errors A syntax error refers to a mistake in a
statement's syntax.

A Logical Error in a program causes


Logical Errors unexpected results or operation but
not failure.
A Runtime Error is a problem that
Runtime arises when the program is
Errors
executed.
3/11/2018 5
KLE Society’s
B. V. Bhoomaraddi. College of
Engineering and Technology

Syntax Errors
A syntax error refers to a mistake in a
statement's syntax.

Such errors are indicated by the compilers.


Examples:
1. Missing semi-colon
2.When the rules of the C programming language
Unnecessary semi-colon terminator
3. are
Undeclared variable
not followed, name will show syntax
the compiler
4. errors.
Mis-matched parentheses
5. Left-side of assignment is not a defined memory
location
6. Return statement missing
KLE Society’s
B. V. Bhoomaraddi. College of
Engineering and Technology
Syntax Errors
Examples: Unnecessary semi-colon terminator

if (age >=60 );
printf(“Senior citizen”); Unnecessary
semi-colon
If (age >=3 && age <=5 )
printf(“LKG to UKG”);
else if (age >=6 && age<=12);
printf(“Primary School”);
else if (age >=13 && age <=16)
printf(“High School”);

3/11/2018 K. L. E. Technological University, Hubli- 7


31.
KLE Society’s
B. V. Bhoomaraddi. College of
Engineering and Technology

Examples: Left-side of assignment is not a defined


memory location
#include<stdio.h>
main()
{
int n1 =10, n2=40;
result = n1,+n2; //result not declared
printf(“addition result = %d\n”, result);
}

3/11/2018 K. L. E. Technological University, Hubli- 8


31.
KLE Society’s
Syntax Errors
B. V. Bhoomaraddi. College of
Engineering and Technology

Left-side of assignment is not a defined


memory location
Where is the error?

if (n%7=0)  Why?
printf(“%d is divisible by 7\n”, n);
else
printf(“%d is NOT divisible by 7\n”, n);

n%7=0 We are trying to assign 0 to n%7.


Lvalue is not a memory location.
We are comparing for equality. Use ==
3/11/2018 K. L. E. Technological University, Hubli- 9
31.
KLE Society’s
B. V. Bhoomaraddi. College of
Engineering and Technology

Techniques for Identifying Syntax


errors
1. Reading code statements (walkthroughs)

2. Compiler error messages (compiler output)

3/11/2018 K. L. E. Technological University, Hubli- 10


31.
KLE Society’s
B. V. Bhoomaraddi. College of
Engineering and Technology

Identify the syntax errors (if any) and rewrite


Time: 5 minutes Individual

Sl.No
C Statement
1 printf(“%d %d” i,j);
2 printf(“%d %d”, “i=“,i);
3 scanf(“%d”, &i, &j);
4 printf(“%d %d”, 10);
5 printf(“%d %d”, 1 % 2, ‘I’);
6 Int A,B;
7 Float rate = 3.7:
3/11/2018 K. L. E. Technological University, Hubli- 11
31.
KLE Society’s
B. V. Bhoomaraddi. College of
Engineering and Technology

Logical Errors
A Logical Error in a program causes
unexpected results or operation but not
failure.
Cannot be checked by the compilers, as the
program is syntactically correct.
These are usually the hardest to debug
because they do not cause the program to
fail completely.
3/11/2018 K. L. E. Technological University, Hubli- 12
31.
KLE Society’s
B. V. Bhoomaraddi. College of
Engineering and Technology

Logical Errors??
1. Use of = instead of ==
2. iteration without a body (for/while followed by a
semi-colon)
3. uninitialized variable
4. infinite iteration
5. incorrect operator order in a compound expression
6. dangling else
7. off-by-one iteration
8. integer division and truncation
9. mis-matched data types
10. & instead of &&
3/11/2018 K. L. E. Technological University, Hubli- 13
31.
KLE Society’s
B. V. Bhoomaraddi. College of
Engineering and Technology

Identify the logic errors(if any) and rewrite


Time: 10 minutes Individual Activity
1. int average(int a, int b) 3. a==b+c+d;
{
return a + b / 2;
}

2. int a=10;
if(a=2)
{
printf(“The value of a is 2\n”);
}
else
{
printf(“The value of a is not 2\n”);
}
3/11/2018 K. L. E. Technological University, Hubli- 14
31.
KLE Society’s
B. V. Bhoomaraddi. College of
Engineering and Technology

Runtime Errors

• A Runtime Error is a problem that arises when


the program is executed.

• E.g. divide by zero, array out of bounds,


segmentation fault.

3/11/2018 K. L. E. Technological University, Hubli- 15


31.
KLE Society’s
B. V. Bhoomaraddi. College of
Engineering and Technology

Example for Runtime Error


#include<stdio.h> The denominator is read from
the user. User can enter any
main() value.

{
int a,b;
float r;
scanf(“%d %d”, &a,&b);
r=a/b;
printf(“Result=%d”,r);
}
3/11/2018 K. L. E. Technological University, Hubli- 16
31.
KLE Society’s
B. V. Bhoomaraddi. College of
Engineering and Technology

Identify the runtime errors(if any) and rewrite


1. #include<stdio.h>
main()
{
int a;
scanf(“%d”,a);
printf(“%d”,a);
}

3/11/2018 K. L. E. Technological University, Hubli- 17


31.
KLE Society’s
B. V. Bhoomaraddi. College of
Engineering and Technology

STEPS IN DEBUGGING

3/11/2018 K. L. E. Technological University, Hubli- 18


31.
KLE Society’s
B. V. Bhoomaraddi. College of
Engineering and Technology

Basic steps in debugging


 Recognize that a bug or error exists. How?

 Identify the cause of the bug or error.

 Determine a fix for the bug or error.

 Apply the fix and test it.


3/11/2018 19
KLE Society’s
B. V. Bhoomaraddi. College of
Engineering and Technology

Let us see how to apply the steps of debugging.


/* Program to read a value from keyboard and print the value on output
screen */

#include<stdio.h> #include<stdio.h>
main() main()
{ {
int a; int a;
scanf (“%d”, &a) scanf (“%d”, &a);
printf (“%d, &a); printf (“%d” a);
} }

3/11/2018 K. L. E. Technological University, Hubli- 20


31.
KLE Society’s
B. V. Bhoomaraddi. College of
Engineering and Technology

Debugging: Compiler error messages


• The error messages that the compiler
generates help us locate the error and identify
the error.
C Statement Compiler Error message
int a semicolon missing
a=b+c: Expected semicolon at the end of line
Printf(“Hello”); Undefined Printf
int a,b=4,c=2; Undefined b_c
a=b_c;
3/11/2018 21
KLE Society’s
B. V. Bhoomaraddi. College of
Engineering and Technology

END OF SESSION

3/11/2018 K. L. E. Technological University, Hubli- 22


31.

Vous aimerez peut-être aussi