The #define directive
A preprocessor define directive directs the preprocessor to replace all subsequent occurrences of a macro with specified replacement tokens.
The
#define
directive can contain:
The following are some differences between
#define
and
the const
type qualifier: - The
#define
directive can be used to create a name for a numerical, character, or string constant, whereas aconst
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 information