Token-Pasting Operator (##)

Beema, Vishnu (IE10) Vishnu.Beema@Honeywell.com
Wed Mar 24 13:53:00 GMT 2010


Hi Eljay,

Can you comment more on the below statement

" You cannot token paste X substitution argument conjoined to . (dot).
That does not form a token, it forms two tokens."

Since I tried a sample program in Microsoft Visual Soft C++

		#define SET_STR_OUTPUT(X, Y)	str ## X ## .str ## Y ##
Var

		typedef struct
		{
			char str1Var;
			char str2Var;
			char str3Var;
		}str;
		
		Void main()
		{
			str str1, str2, str3;
		
			SET_STR_OUTPUT(1, 1) = 5; 
			
			printf("%d\n", str1.str1Var);
  		}

And it worked fine and the output is displayed as 5.

Thanks & Regards
Vishnu

-----Original Message-----
From: John (Eljay) Love-Jensen [mailto:eljay@adobe.com] 
Sent: Wednesday, March 24, 2010 6:12 PM
To: Beema, Vishnu (IE10); gcc-help@gcc.gnu.org
Subject: RE: Token-Pasting Operator (##)

Hi Vishnu,

To use token pasting preprocessor operator, you must be pasting a token
and a token to form a single new token.  You cannot be pasting two token
together.

#define DO_SET_1(X, Y)      PORT ## X ##.OUTSET = PIN ## Y ## _bm
DO_SET_1(D, 0);

I am going to list out the expansion with one token per line:

PORTD
.
OUTSET
=
PIN0_bm

Notice in your macro you have this token pasting:

X ## .

You cannot token paste X substitution argument conjoined to . (dot).
That does not form a token, it forms two tokens.

If you change your #define macro to:

#define DO_SET_1(X, Y)      PORT ## X .OUTSET = PIN ## Y ## _bm

Then you will no longer be trying to form an invalid token paste between
X and . (dot).

Sincerely,
--Eljay



More information about the Gcc-help mailing list