The ## operator
The ## (double number sign) operator concatenates two tokens in a macro invocation (text and/or arguments) given in a macro definition.
If a macro XY was defined using the following
directive:
#define XY(x,y) x##y
the last token of the argument for x is concatenated with the first token of the argument for y.
Use the ## operator according to the
following rules:
- The ## operator cannot be the very first or very last item in the replacement list of a macro definition.
- The last token of the item in front of the ## operator is concatenated with first token of the item following the ## operator.
- Concatenation takes place before any macros in arguments are expanded.
- If the result of a concatenation is a valid macro name, it is available for further replacement even if it appears in a context in which it would not normally be available.
- If more than one ## operator and/or # operator appears in the replacement list of a macro definition, the order of evaluation of the operators is not defined.
The following examples demonstrate the use of the ## operator:
#define ArgArg(x, y) x##y
#define ArgText(x) x##TEXT
#define TextArg(x) TEXT##x
#define TextText TEXT##text
#define Jitter 1
#define bug 2
#define Jitterbug 3
Invocation | Result of macro expansion |
---|---|
ArgArg(lady, bug) | ladybug |
ArgText(con) | conTEXT |
TextArg(book) | TEXTbook |
TextText | TEXTtext |
ArgArg(Jitter, bug) | 3 |