|
|
coherent
#define Preprocessing Directive #define
Define an identifier as a macro
#ddeeffiinnee _i_d_e_n_t_i_f_i_e_r _l_p_a_r_e_n _i_d_e_n_t_i_f_i_e_r-_l_i_s_t9_o_p_t8 ) _r_e_p_l_a_c_e_m_e_n_t-_l_i_s_t
The preprocessing directive #define tells the C preprocessor to
regard identifier as a macro.
#define can define two kinds of macros: object-like, and
function-like.
An object-like macro has the syntax
#define _i_d_e_n_t_i_f_i_e_r _r_e_p_l_a_c_e_m_e_n_t-_l_i_s_t
This type of macro is also called a manifest constant. The
preprocessor searches for identifier throughout the text of the
translation unit, and replaces it with the elements of
replacement-list, which is then rescanned for further macro sub-
stitutions.
For example, consider the directive:
#define BUFFERSIZE 75
When the preprocessor reads the line
malloc(BUFFERSIZE);
it replaces it with:
malloc(75);
A given identifier is replaced only once by a given replacement-
list. This is to prevent such code as
#define FOO FOO
or
COHERENT Lexicon Page 1
#define Preprocessing Directive #define
#define FOO BAR
#define BAR FOO
from generating an infinite loop.
A function-like macro is more complex. It has the syntax:
#define _i_d_e_n_t_i_f_i_e_r _l_p_a_r_e_n _i_d_e_n_t_i_f_i_e_r-_l_i_s_t9_o_p_t8 ) _r_e_p_l_a_c_e_m_e_n_t-_l_i_s_t
The preprocessor looks for identifier, which is a macro that
resembles a function in that it is followed by a pair of paren-
theses that may enclose an identifier-list. It replaces iden-
tifier with the contents of replacement-list, up to the first
lparen `(' within replacement-list.
The preprocessor then examines identifier-list for further mac-
ros, which it expands. The modified identifier-list is then
replaced with the rest of replacement-list. Pairs of parentheses
that are nested between the lparen that begins replacement-list
and the `)' that ends it are copied into identifier-list as
literal characters. The identifiers within identifier-list are
preserved after it has been modified by replacement-list. The
only exceptions are identifiers that are prefixed by the
preprocessing operators # or ##; these are handled appropriately.
For example, the consider the macro:
#define display(x) show((long)(x), #x)
When the preprocessor reads the following line
display(abs(-5));
it replaces it with the following:
show((long)(abs(-5)), "abs(-5)");
When an argument to a function-like macro contains no preproces-
sing tokens, or when an argument to a function-like macro con-
tains a preprocessing token that is identical to a preprocessing
directive, the behavior is undefined.
***** Example *****
For an example of using a function-like macro in a program, see
#.
COHERENT Lexicon Page 2
#define Preprocessing Directive #define
***** See Also *****
#, ##, #undef, C preprocessor
***** Notes *****
A macro expansion always occupies exactly one line, no matter how
many lines are spanned by the definition or the actual
parameters. If you have defined macros that span more than one
line, you must either redefine them to occupy one line, or some-
how embed the newline character within the macro itself; other-
wise, the macro will not expand correctly.
A macro definition can extend over more than one line, provided
that a backslash `\' appears before the newline character that
breaks the lines. The size of a #define directive is therefore
limited by the maximum size of a logical source line, which can
be up to at least 509 characters long.
Some implementations allowed a user to re-define a macro with a
new #define directive. The Standard, however, allows only a
``benign'' redefinition; that is, the body of the new definition
must exactly match the old definition, including parameter names
and white space.
COHERENT Lexicon Page 3
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.