You are on page 1of 10

C Programming

DEBUGGING
What is debugging?
• Debugging is not getting the compiler to
compile your code without syntax errors or
linking errors.
• The most common types of mistakes
when programming are:
– Programming without thinking
– Writing code in an unstructured manner
“The sooner I start coding the
sooner a working program will
be produced.”
Debugging Hints
• To solve a problem, you must understand
it.
• Make life easier for yourself:
– Indent your code properly.
– Use meaningful variable names.
– Print debugging to stderr or stdout, but not
both, or your debugging output may be
reordered.
– Develop incrementally. Test as you go.
Debugging Hints
• Get the compiler to help.
• Use programming manual.
• When the program does not work, pause,
read the output very carefully, read your
code, and think first.
• If there is not enough evidence to find and
• understand a problem, you need to gather
more evidence.
Common C Errors
• Missing break in a switch statement
• Using = instead of ==
if (a = 0) { ..... }
• Incorrect use of semicolon
while (x < 10);
x++;
• Missing parameters
printf(“The value of a is %d\n”);
• Wrong parameter type in printf, scanf, etc:
double num;
printf(“The value of n is %d\n”, num);
Exercises
#Include<stdio.h>

int count;
int values[10];

main()
{
for( count = 0; count < 10; count++ ){
value[count] = 5;
printf("%d/n", values[count]):

getch();
}

Debug the program and determine the output.


#include<stdio.h>

int numbers[] = (16, 2, 77, 40, 12071);


int n, result=0;

main()
{
for ( n=0 : n<5 ; n++ );
{
result += Numbers[n];
}
printf(“d",result);

getch();
}

Debug the program and determine the output.

You might also like