< previous page page_365 next page >

Page 365
LISTING 21.5 PRINTING VALUES INDEBUG MODE

d5ef64f4d3250b96ba5c07ca5bbc2f56.gif
1:     // Listing 21.5 - Printing values in DEBUG mode
2:     #include <iostream.h>
3:     #define DEBUG
4:
5:     #ifndef DEBUG
6:     #define PRINT(x)
7:     #else
8:     #define PRINT(x) \
9:        cout << #x << :\t << x << endl;
10:    #endif
11:
12:
13:    int main()
14:    {
15:       int x = 5;
16:       long y = 738981;
17:       PRINT(x);
18:       for (int i = 0; i < x; i++)
19:       {
20:          PRINT(i);
21:       }
22:
23:       PRINT (y);
24:       PRINT(Hi.);
25:       int *px = &x;
26:       PRINT(px);
27:       PRINT (*px);
28:       return 0;
29:    }

Output:
x:            5
i:            0
i:            1
i:            2
i:            3
i:            4
y:            73898
Hi. :       Hi.
px:           0x2100 (You may recieve a value other than 0x2100)
*px:          5
Analysis: The macro on lines 510 provides printing of the current value of the supplied parameter. Note that the first thing fed to cout is the stringized version of the parameter; that is, if you pass in x, cout receives x.
Next cout receives the quoted string :\t, which prints a colon and then a tab. Third, cout receives the value of the parameter (x) and then finally end1, which writes a new line and flushes the buffer.

 
< previous page page_365 next page >

If you like this book, buy it!