Its Question For Embedded Programmers

Asked By 30 points N/A Posted on -
qa-featured

#define cat(x,y) x##y concatenates x to y.

But cat(cat(1,2),3) does not expand but gives preprocessor warning. Why?

SHARE
Answered By 0 points N/A #155125

Its Question For Embedded Programmers

qa-featured

Hi Sarmadmunir,

The Preprocessor warning comes because of cat(cat(1,2),3) , it becomes cat(1,2)##3 and ## tries to make a token out of ) and 3 and you cannot make a token out of those two.

Your solution is this : you need to take

#define xcat(x,y) x ## y
#define cat(x,y) xcat(x,y)

then your problem goes to xcat(cat(1,2),3) and it won't give you a warning anymore.Now since the 2 ## are gone , the formula becomes xcat(xcat(1,2),3) and even further the inner expansion goes to xcat(1##2,3) , next to xcat(12,3) , finally getting to 12##3
and then your answer is 123.

Now the conclusion is that you tried to use a macro , which was not intended to be used as a function.I'm guessing you wanted to get out 1##2##3 but it didn't work.Well , I hope this works for you and solves your problem.

Best Regards, Mamangun Ceilo

Login/Register to Answer

Related Questions