ARM Technical Support Knowledge Articles

C MACRO EXPANSION PROBLEM WITH PARAMETERS

Applies to: General Topics

Answer


Information in this article applies to:


QUESTION

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?

ANSWER

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

MORE INFORMATION

SEE ALSO

Article last edited on: 2005-06-06 09:30:57

Rate this article

[Bad]
|
|
[Good]
Disagree? Move your mouse over the bar and click

Did you find this article helpful? Yes No

How can we improve this article?

Link to this article
Copyright © 2011 ARM Limited. All rights reserved. External (Open), Non-Confidential