A preprocessing macro has one of the following two forms.
#define macro_name replacement_list
#define macro_name(identifier,identifer,...identifier) replacement_list
- The latter form is referred to as a function-like macro. For such macros, no space can
occur between the macro name and the left parenthesis immediately following.
- The macro replacement list is a sequence of tokens. White space surrounding a
macro's replacement list is discarded when the preprocessor analyses the macro
definition. White space separating tokens in a replacement list (and not
within a string literal or character constant) is replaced with a single space.
- Macros can be redefined only if the second and subsequent definitions exactly
match the first. For function-like macros this means that the macro name, the
number and spelling of the parameters and the replacement lists are identical.
- Once a macro has been defined to the preprocessor, the replacement list is
used to replace subsequent occurrences of the macro name.
- The number of parameters supplied in a call to a function-like macro must
match the number of parameters in its definition. The parameters specified on a
call (to the macro) are tokens separated by commas. Commas in quoted strings and
character constants and commas enclosed within nested parentheses do not
delineate between parameters.
- After the parameters of a call to a function-like macro have been identified,
substitutions in the replacement list are made. Unless preceded by a
# or adjacent to a ##,
a formal parameter that is found in the replacement list is replaced by its
corresponding actual parameter (after any macros found in the actual parameter
have been expanded).
- After replacing a macro, the resultant replacement list is rescanned to determine
if more macros are present (and hence require expansion). If during the
rescanning, the name of the original macro (with which the replacement list is
associated) is encountered, it is not replaced. All other macros in the replacement list are replaced as usual.
- A fully processed replacement list is not treated as a preprocessing directive even
if it has the form of one.
Given the simple macro definition
#define size 256
expands as follows.
Given the macro definitions:
#define ten 10
#define twenty 20
#define sum(parameterA,parameterB) parameterA + parameterB
expands as shown below.
10 + 20
Note that the parameters specified on the call to the function-like macro sum
are themselves macros. The actual parameters specified on the call are bound
to their formal counterparts. The actual parameters are expanded (being
themselves macros) prior to substitution into the macro replacement list.