6.10.3 Macro replacement

Previous Table of Contents "New C Standard" commentary

1918 Two replacement lists are identical if and only if the preprocessing tokens in both have the same number, ordering, spelling, and white-space separation, where all white-space separations are considered identical.

1919 An identifier currently defined as an object-like macro shall not be redefined by another #define preprocessing directive unless the second definition is an object-like macro definition and the two replacement lists are identical.

1920 Likewise, an identifier currently defined as a function-like macro shall not be redefined by another #define preprocessing directive unless the second definition is a function-like macro definition that has the same number and spelling of parameters, and the two replacement lists are identical.

1921 There shall be white-space between the identifier and the replacement list in the definition of an object-like macro.

1922 If the identifier-list in the macro definition does not end with an ellipsis, the number of arguments (including those arguments consisting of no preprocessing tokens) in an invocation of a function-like macro shall equal the number of parameters in the macro definition.

1923 Otherwise, there shall be more arguments in the invocation than there are parameters in the macro definition (excluding the ...).

1924 There shall exist a ) preprocessing token that terminates the invocation.

1925 The identifier __VA_ARGS__ shall occur only in the replacement-list of a function-like macro that uses the ellipsis notation in the argumentsparameters.

1926 A parameter identifier in a function-like macro shall be uniquely declared within its scope.

1927 The identifier immediately following the define is called the macro name.

1928 There is one name space for macro names.

1929 Any white-space characters preceding or following the replacement list of preprocessing tokens are not considered part of the replacement list for either form of macro.

1930 If a # preprocessing token, followed by an identifier, occurs lexically at the point at which a preprocessing directive could begin, the identifier is not subject to macro replacement.

1931 A preprocessing directive of the form

  # define identifier replacement-list new-line

defines an object-like macro that causes each subsequent instance of the macro name146) to be replaced by the replacement list of preprocessing tokens that constitute the remainder of the directive.

1932 The replacement list is then rescanned for more macro names as specified below.

1933 A preprocessing directive of the form

  # define identifier lparen identifier-listopt ) replacement-list new-line
  # define identifier lparen ... ) replacement-list new-line
  # define identifier lparen identifier-list , ... ) replacement-list new-line

defines a function-like macro with arguments, parameters, whose use is similar syntactically to a function call.

1934 The parameters are specified by the optional list of identifiers, whose scope extends from their declaration in the identifier list until the new-line character that terminates the #define preprocessing directive.

1935 Each subsequent instance of the function-like macro name followed by a ( as the next preprocessing token introduces the sequence of preprocessing tokens that is replaced by the replacement list in the definition (an invocation of the macro).

1936 The replaced sequence of preprocessing tokens is terminated by the matching ) preprocessing token, skipping intervening matched pairs of left and right parenthesis preprocessing tokens.

1937 Within the sequence of preprocessing tokens making up an invocation of a function-like macro, new-line is considered a normal white-space character.

1938 The sequence of preprocessing tokens bounded by the outside-most matching parentheses forms the list of arguments for the function-like macro.

1939 The individual arguments within the list are separated by comma preprocessing tokens, but comma preprocessing tokens between matching inner parentheses do not separate arguments.

1940 If there are sequences of preprocessing tokens within the list of arguments that would otherwise act as preprocessing directives147), the behavior is undefined.

1941 If there is a ... in the identifier-list in the macro definition, then the trailing arguments, including any separating comma preprocessing tokens, are merged to form a single item: the variable arguments.

1942 The number of arguments so combined is such that, following merger, the number of arguments is one more than the number of parameters in the macro definition (excluding the ...).

1943 146) Since, by macro-replacement time, all character constants and string literals are preprocessing tokens, not sequences possibly containing identifier-like subsequences (see 5.1.1.2, translation phases), they are never scanned for macro names or parameters.

1944 147) Despite the name, a non-directive is a preprocessing directive.

Next

Created at: 2008-01-30 02:39:45 The text from WG14/N1256 is copyright © ISO