Reputation: 1097
I came across this question in an Embedded interview question set.
#define cat(x,y) x##y
concatenates x
to y
. But cat(cat(1,2),3)
does not expand but gives preprocessor warning. Why?
Does C not encourage Recursive Macro expansions ? My assumption is the expression should display 1##2##3
. Am i wrong ?
Upvotes: 4
Views: 1370
Reputation: 106082
The problem is that cat(cat(1,2),3)
isn't expanded in a normal way which you expect that cat(1,2)
would give 12
and cat(12, 3)
would give 123
.
Macro parameters that are preceded or followed by ##
in a replacement list aren't expanded at the time of substitution. As a result, cat(cat(1,2),3)
expands to cat(1,2)3
, which can't be further expanded since there is no macro named cat(1,2)3
.
So the simple rule is that, macros whose replacement lists depends on ##
usually can't be called in a nested fashion.
Upvotes: 3