Name
#define
Synopsis
The #define
directive is used
to define
macros.
Syntax:
definename
[(parameter_list)
][replacement_text]
The preprocessor replaces each occurrence of
name
or
name(parameter_list)
in the subsequent
source code with replacement_text
.
Examples:
#define BUF_SIZE 512 // Symbolic constant #define MAX(a,b) ((a) > (b) ? (a) : (b))
These directives define the macros BUF_SIZE
and
MAX
. If the replacement text is a constant
expression, the macro is also called a symbolic
constant
. Macros can also be nested; a macro,
once defined, can be used in another macro definition.
In the previous example, the parentheses are necessary in order for
the substitution to be performed correctly when
MAX
is used in an expression, or when complex
expressions replace the parameters a
and
b
. For example, the preprocessor replaces the
macro invocation:
result = 2 * MAX( x, y & 0xFF );
with:
result = 2 * ( (x) > (y & 0xFF) ? (x) : (y & 0xFF) );
Get C Pocket Reference now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.