The #define directive can be used to assign mnemonic names to constants. There are two forms:
#define identifier expression // parameter-free form |
The #define directive substitutes expression for all further found entries of identifier in the source text. The identifier is replaced only if it is a separate token. The identifier is not replaced if it is part of a comment, part of a string, or part of another longer identifier.
The constant identifier is governed by the same rules as variable names. The value can be of any type:
#define ABC 100 |
expression can consist of several tokens, such as keywords, constants, constant and non-constant expressions. expression ends with the end of the line and can't be transferred to the next line.
Example:
#define TWO 2 |
With the parametric form, all the subsequent found entries of identifier will be replaced by expression taking into account the actual parameters. For example:
// example with two parameters a and b |
Be sure to enclose parameters in parentheses when using the parameters in expression, as this will help avoid non-obvious errors that are hard to find. If we rewrite the code without using the brackets, the result will be different:
// example with two parameters a and b |
When using the parametric form, maximum 8 parameters are allowed.
// correct parametric form |
See also
Identifiers, Character Constants