This is the simplest form of macro subsitution. We have seen:
#define FALSE 0 #define TRUE 1
Similarly, you might have
#define PI 3.142
although
#define PI ( (double) (3.142) )
is better. Just to emphasise that this is a preprocessor operation, note that everything to the end of the line is substituted, so in
#define TRUE 1 /* Any non-zero value will do */
the comment statement will be included: wherever you have TRUE in your program you will henceforth get:
1 /* Any non-zero value will do */
You can write longer macros, extending over several lines. Use the backslash symbol at the end of each line except the last:
#define BIG_MACRO first line \\ second_line \\ last_line
You can also write macros with arguments:
#define MAX(A,B) ( (A) > (B) ? (A) : (B) )
With this definition, a line
x= MAX(p+q, r+s);
will be replaced by:
x= ( (p+q) > (r+s) ? (p+q) : (r+s) );
Note the use of `extra' brackets around A and B to ensure that, whatever is written there is correctly evaluated. Consider what you would get with the unsound form:
#define square(x) x*x
and square(a + 1).
Finally, #undef causes the definition to be forgotten.
maspjw@