#define BUFFER_SIZE 256
as a textual assignment giving the symbol BUFFER_SIZE a value "256". Symbols defined with cpp are traditionally given upper case names to distinguish them from C identifiers. This symbol can be used later in the input, as in
This use of cpp to name constants, rather than writing these magic numbers inline, makes a program easier to read and maintain, especially if there is more than one occurrence of BUFFER_SIZE all of which must all have the same value.
Cpp macros can have parameters:
#define BIT(n) (1<<(n))
This can be used with any appropriate actual argument:
msb = BIT(nbits-1);
Note the parentheses around the "n" in the definition of BIT. Without these, operator precedence might mean that the expression substituted in place of n might not be interpreted correctly (though the example above would be OK).
Cpp also supports conditional compilation with the use of
#ifdef SYMBOL ... #else ... #endif and #if EXPR ... #else ... #endif
constructs, where SYMBOL is a Cpp symbol which may or may not be defined and EXPR is an arithmetic expression involving only Cpp symbols, constants and C operators which Cpp can evaluate to a constant at compile time.
(01 Jun 2001)
|Bookmark with:||word visualiser||Go and visit our forums|