GENERAL: C MACRO EXPANSION PROBLEM WITH PARAMETERS
Information in this article applies to:
I have a problem with macro expansion and a parameter list in the following source code.
#define CONCAT(A,B) A##B #define RES(R) R #define MSO 1 CONCAT ( TR_,RES(MSO) ); // should give TR_1, but results in TR_RES(1) CONCAT ( RES(MSO),_TR ); // should give 1_TR, and works OK
The parameters of a C macro are replaced if they are macros itself. Why doesn't this work?
In general, you are correct: macro parameters are replaced if they are macros. However, the ## operator implies a different behavior and inserts a placeholder which is replaced later.
To get the result that you expect, you must add another macro and rewrite your macros as follows:
#define CONCAT(a, b) XCAT(a, b) #define XCAT(a, b) a ## b #define RES(R) R #define MSO 1 CONCAT ( TR_,RES(MSO) ); // gives now TR_1 CONCAT ( RES(MSO),_TR ); // gives 1_TR as before
Last Reviewed: Monday, June 6, 2005
of your data.