Next: #undef Up: The C Preprocessor Previous: The C Preprocessor


Use this to define constants or any macro substitution. Use as follows:

We can also define small ``functions'' using #define. For example max. of two variables:

#define max(A,B) ( (A) > (B) ? (A):(B))

? is the ternary operator in C.

Note: that this does not define a proper function max.

All it means that wherever we place max(C,D) the text gets replaced by the appropriate definition. [ = any variable names - not necessarily C and D]

So if in our C code we typed something like:

x = max(q+r,s+t);

after preprocessing, if we were able to look at the code it would appear like this:

x = ( (q+r) > (r+s) ? (q+r) : (s+t));

Other examples of #define could be:

NOTE: The last macro LEFT_SHIFT_8 is only valid so long as replacement context is valid i.e. x = y LEFT_SHIFT_8.
Wed Sep 14 10:06:31 BST 1994