C.4. Use of the Preprocessor

The contents of every header (*.h) file must be enclosed in the pre-processor directives shown above.

The preprocessor can be used for:

An example of a conditional arising from system limits:

#include <limits.h>

#error "need int wider than char"

C.4.1. Constants

For constants, consider using enum and variables instead of #define, which is rarely visible in debuggers. For example:

enum { Red = 0xF00, Blue = 0x0F0, Green = 0x00F };
static const float pi = 3.14159265358;

C.4.2. Macros

Macros should avoid side effects. If possible, mention each argument exactly once. Put parentheses around all arguments. When the macro is an expression, put parentheses around the whole macro body. If the macro is the inline expansion of some function then capitalise the name and precede it with M:

#define MAJFUBAR(X) \
    (save = (X),                    \
    dosomethingwith(X),             \
    (X) = save);

Try to write macros so that they are syntactically expressions i.e. so you can put a semi-colon at the end of their use.