20.09.2015 Views

Programming in C

Kochan - ProgramminginC

Kochan - ProgramminginC

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

390 Chapter 18 Debugg<strong>in</strong>g Programs<br />

Program 18.1<br />

Cont<strong>in</strong>ued<br />

<strong>in</strong>t ma<strong>in</strong> (void)<br />

{<br />

<strong>in</strong>t i, j, k, nread;<br />

nread = scanf ("%d %d %d", &i, &j, &k);<br />

#ifdef DEBUG<br />

fpr<strong>in</strong>tf (stderr, "Number of <strong>in</strong>tegers read = %i\n", nread);<br />

fpr<strong>in</strong>tf (stderr, "i = %i, j = %i, k = %i\n", i, j, k);<br />

#endif<br />

}<br />

pr<strong>in</strong>tf ("%i\n", process (i, j, k));<br />

return 0;<br />

Program 18.1<br />

Output<br />

1 2 3<br />

Number of <strong>in</strong>tegers read = 3<br />

i = 1, j = 2, k = 3<br />

6<br />

Program 18.1<br />

Output (Rerun)<br />

1 2 e<br />

Number of <strong>in</strong>tegers read = 2<br />

i = 1, j = 2, k = 0<br />

3<br />

Note that the value displayed for k can be anyth<strong>in</strong>g because its value was not set by the<br />

scanf call and it was not <strong>in</strong>itialized by the program.<br />

The statements<br />

#ifdef DEBUG<br />

fpr<strong>in</strong>tf (stderr, "Number of <strong>in</strong>tegers read = %i\n", nread);<br />

fpr<strong>in</strong>tf (stderr, "i = %d, j = %d, k = %d\n", i, j, k);<br />

#endif<br />

are analyzed by the preprocessor. If the identifier DEBUG has been previously def<strong>in</strong>ed<br />

(#ifdef DEBUG), the preprocessor sends the statements that follow up to the #endif (the<br />

two fpr<strong>in</strong>tfs) to the compiler to be compiled. If DEBUG hasn’t been def<strong>in</strong>ed, the two

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!