Programming in C

(Barry) #1

390 Chapter 18 Debugging Programs


int main (void)
{
int i, j, k, nread;

nread = scanf ("%d %d %d", &i, &j, &k);

#ifdef DEBUG
fprintf (stderr, "Number of integers read = %i\n", nread);
fprintf (stderr, "i = %i, j = %i, k = %i\n", i, j, k);
#endif

printf ("%i\n", process (i, j, k));
return 0;
}

Program 18.1 Output

1 2 3
Number of integers read = 3
i = 1, j = 2, k = 3
6

Program 18.1 Output (Rerun)

1 2 e
Number of integers read = 2
i = 1, j = 2, k = 0
3

Note that the value displayed for kcan be anything because its value was not set by the
scanfcall and it was not initialized by the program.
The statements
#ifdef DEBUG
fprintf (stderr, "Number of integers read = %i\n", nread);
fprintf (stderr, "i = %d, j = %d, k = %d\n", i, j, k);
#endif
are analyzed by the preprocessor. If the identifier DEBUGhas been previously defined
(#ifdef DEBUG), the preprocessor sends the statements that follow up to the #endif(the
two fprintfs) to the compiler to be compiled. If DEBUGhasn’t been defined, the two

Program 18.1 Continued
Free download pdf