About ## preprocessor in C

c-preprocessor

Solution

I tested this using both GCC and Clang.

GCC gives the error:

test.c:6:1: error: pasting ")" and "3" does not give a valid preprocessing token

Clang gives the error:

test.c:6:11: error: pasting formed ')3', an invalid preprocessing token
  int b = cat(cat(1,2),3);

What appears to be happening is that the compiler wraps the result of `cat(1,2)` in parentheses as soon as it is expanded; so when you call `cat(1,2)` in your code, it really gives you `(12)`. Then, calling `cat((12),3)` again leads to `((12)3)`, which is not a valid token, and this results in a compile error.

The common opinion is "when using the token-pasting operator (##), you should use two levels of indirection" (i.e., use your `xcat` workaround). See Why do I need double layer of indirection for macros? and What should be done with macros that need to paste two tokens together?.

Problem

Given ``` #define cat(x,y) x##y ``` The call `cat(a,1)` returns `a1`, but `cat(cat(1,2),3)` is undefined. However if I also define `#define xcat(x,y) cat(x,y)`, then the result of `xcat(xcat(1,2),3)` is now `123`. Can anybody please explain in detail why this is so?

Original source

Related problems