Table Of Contents
Previous Section Next Section

Program 76: Double Trouble

The macro DOUBLE is designed to double the value of its argument. The test program prints out the DOUBLE values of the numbers for 1 through 5. Yet something goes wrong. What's happening?

  1 /************************************************
  2  * Double -- Print double table.                *
  3  *                                              *
  4  * Print the numbers 1 through 5 and their       *
  5  *      doubles.                                *
  6  ************************************************/
  7 #include <iostream>
  8
  9 /************************************************
 10  * DOUBLE -- Given a number return its double. *
 11  ************************************************/
 12 #define DOUBLE(x) (x * 2)
 13
 14 int main()
 15 {
 16     int i;       // Number to print and to double
 17
 18     for (i = 0; i < 5; ++i) {
 19         std::cout << "The double of " << i+1 <<
 20             " is " << DOUBLE(i+1) << std::endl;
 21     }
 22
 23     return (0);
 24 }

(Next Hint 133. Answer 46.)

Table Of Contents
Previous Section Next Section