Testing and Debugging
Diagnostic Log
A debug flag can print the values that explain how a result was produced.
debug flag
A flag lets diagnostic output be turned on while leaving the normal result unchanged.
trace value
Printing intermediate values can confirm which path the program followed.
Diagnostic Log
diagnostic_log.c
Replay: real traced execution (multi-file project)
#include <stdio.h>
int main(void) {
int debug = 1;
int width = 6;
int height = 4;
int area = width * height;
if (debug) {
printf("debug width=%d height=%d\n", width, height);
}
printf("area=%d\n", area);
return 0;
}
#include <stdio.h>
int main(void) {
int debug = 0;
int width = 6;
int height = 4;
int area = width * height;
if (debug) {
printf("debug width=%d height=%d\n", width, height);
}
printf("area=%d\n", area);
return 0;
}
debug ← 1, width ← 6, height ← 4, area ← 24
3int main(void) {4 int debug→ 1 = 1; //@debug=0, 15 int width→ 6 = 6;6 int height→ 4 = 4;7 int area→ 24 = width6 * height4;if (debug)
9if (debug1) {10 printf("debug width=%d height=%d\n", width6, height4);11}outputdebug width=6 height=4printf("area=%d ", area);
13 printf("area=%d\n", area24);14 return 0;15}outputarea=24
debug ← 0, width ← 6, height ← 4, area ← 24
3int main(void) {4 int debug→ 0 = 0;5 int width→ 6 = 6;6 int height→ 4 = 4;7 int area→ 24 = width6 * height4;89 if (debug) {10 printf("debug width=%d height=%d\n", width, height);11 }1213 printf("area=%d\n", area24);14 return 0;15}outputarea=24