

#define              Preprocessing Directive              #define




Define an identifier as a macro

#ddeeffiinnee _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_t9_o_p_t8 ) _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_t9_o_p_t8 ) _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


