c/preprocessor/replace

The preprocessor supports text macro replacement and function-like text macro replacement.

directives
The directives define the  as a macro, that is they instruct the compiler to replace all successive occurrences of  with, which can be optionally additionally processed. If the identifier is already defined as any type of macro, the program is ill-formed unless the definitions are identical.

Object-like macros
Object-like macros replace every occurrence of a defined with. Version (1) of the directive behaves exactly like that.

Function-like macros
Function-like macros replace each occurrence of a defined with, additionally taking a number of arguments, which then replace corresponding occurrences of any of the  in the.

The syntax of a function-like macro invocation is similar to the syntax of a function call: each instance of the macro name followed by a ( as the next preprocessing token introduces the sequence of tokens that is replaced by the replacement-list. The sequence is terminated by the matching ) token, skipping intervening matched pairs of left and right parentheses.

The number of arguments must be the same as the number of arguments in the macro definition or the program is ill-formed. If the identifier is not in functional-notation, i.e. does not have parentheses after itself, it is not replaced at all.

Version (2) of the directive defines a simple function-like macro.

Version (3) of the directive defines a function-like macro with variable number of arguments. The additional arguments can be accessed using identifier, which is then replaced with arguments, supplied with the identifier to be replaced.

Version (4) of the directive defines a function-like macro with variable number of arguments, but no regular arguments. The arguments can be accessed only with identifier, which is then replaced with arguments, supplied with identifier to be replaced.

Note: if an argument of a function-like macro includes commas that are not protected by matched pairs of left and right parentheses (such as or ), the comma is interpreted as macro argument separator, causing a compilation failure due to argument count mismatch.

and operators
In function-like macros, a operator before an identifier in the  runs the identifier through parameter replacement and encloses the result in quotes, effectively creating a string literal. In addition, the preprocessor adds backslashes to escape the quotes surrounding embedded string literals, if any, and doubles the backslashes within the string as necessary. All leading and trailing whitespace is removed, and any sequence of whitespace in the middle of the text (but not inside embedded string literals) is collapsed to a single space. This operation is called "stringification". If the result of stringification is not a valid string literal, the behavior is undefined.

A operator between any two successive identifiers in the  runs parameter replacement on the two identifiers and then concatenates the result. This operation is called "concatenation" or "token pasting". Only tokens that form a valid token together may be pasted: identifiers that form a longer identifier, digits that form a number, or operators and  that form a. A comment cannot be created by pasting and  because comments are removed from text before macro substitution is considered. If the result of concatenation is not a valid token, the behavior is undefined.

Note: Some compilers offer an extension that allows to appear after a comma and before, in which case the  does nothing when  is non-empty, but removes the comma when  is empty: this makes it possible to define macros such as.

The order of evaluation of and  operators is unspecified.

directive
The directive undefines the, that is it cancels the previous definition of the  by  directive. If the identifier does not have an associated macro, the directive is ignored.

Predefined macros
The following macro names are predefined in any translation unit:

The following additional macro names may be predefined by an implementation:

The values of these macros (except for and ) remain constant throughout the translation unit. Attempts to redefine or undefine these macros result in undefined behavior.