10.7 Programming Exercises
Note that the solutions to all the exercises below can be obtained
using standard C++ syntax such as inline and
enum. In general, using C++ construction is
preferred over using macro definitions. However since this is the
chapter on the preprocessor, macros should be used for these
exercises.
Exercise 10-1: Create a set of macros to define a type called
RETURN_STATUS and the following values:
RETURN_SUCCESS, RETURN_WARNING,
and RETURN_ERROR. Define a macro,
CHECK_RETURN_FATAL,,, that takes a
RETURN_STATUS as its argument and returns true if
you have a fatal error.
Exercise 10-2: Write a macro that returns true if its parameter is divisible by 10
and false otherwise.
Exercise 10-3: Write a macro is_digit that returns true if its
argument is a decimal digit. Write a second macro
is_hex that returns true if its argument is a hex
digit (0-9, A-F, a-f). The second macro should reference the first.
Exercise 10-4: Write a preprocessor macro that swaps two integers. (If
you're a real hacker, write one that does not use a
temporary variable declared outside the macro.)
|