A preprocessor define directive directs the preprocessor to
replace all subsequent occurrences of a macro with specified replacement
tokens.
A preprocessor #define directive has the form:
>>-#--define--identifier--+--------------------------+---------->
| .-,--------------. |
| V | |
'-(----+------------+-+--)-'
'-identifier-'
.----------------.
V |
>----+------------+-+------------------------------------------><
+-identifier-+
'-character--'
The #define directive can contain an object-like
definition or a function-like definition.
#define versus const
- The #define directive can be used to create a name for a
numerical, character, or string constant, whereas a const object of
any type can be declared.
- A const object is subject to the scoping rules for variables,
whereas a constant created using #define is not.
- Unlike a const object, the value of a macro does not appear in
the intermediate source code used by the compiler because they are expanded
inline. The inline expansion makes the macro value unavailable to the
debugger.
- A macro can be used in a constant expression, such as an array bound,
whereas a const object cannot.
-
The compiler does not type-check a macro, including macro arguments.
Related References
