Share
Explore BrainMass

Using C printf Statement to Debug an application

There are many ways to debug a program, perhaps the most elementary is using a printf() statement to show important pieces of information regarding the program execution point, variable value, and loop and branch operation.
The solution also answers the following questions.
How can you use the printf() statement?
Where will you place them?
How is it useful to use #define in aid of printf() debugging?

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 placec 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