Share
Explore BrainMass

Using the Print or printf Statement to Debug an Application

How would you use the print statement to debug an application? (Please provide a simple example)

Solution Preview

A printf() statement can help you look deep into the action of a program. Although it is used to spot errors and faults but remember it is a non-interactive way of finding what is sometimes equivalent of a "needle in a haystack". I list some of the places you can insert a printf() for possible error checking:
1) Immediately after a scanf() to confirm the data you are reading conforms to the type and variable you as expected.

int main(){
int value;

printf("Please enter a value between 0 and 100");
scanf("%d", value);

/*inserting a printf() statement here ...

Solution Summary

A printf() statement can help you look deep into the action of a program. Although it is used to spot errors and faults but remember it is a non-interactive way of finding what is sometimes equivalent of a "needle in a haystack". In this solution you will learn three practical ways to debug an application illustrated with examples.

$2.19