Need help with quoting and the CPP

Paul Stachour stachour at sctc.com
Wed May 1 00:52:26 AEST 1991


>In article <230 at wimpy.nms.gdc.portal.com>, bergquis at nms.gdc.portal.com (Brett Bergquist) writes:
>> Is there any way to expand an argument to a macro and at the same time
>> surround it with quotes (making is a quoted string).


I've had the same problem.  I've not been able to solve it.
I was recently sent a solution by Tom Plum, which follows.
The explanation which follows the example is mine.
The example works, the explanation why might be faulty.


----- Example ----

#define OPEN_OBJECT 12
#define NXSTR(x) #x    /* no-expansion stringize */
#define STR(x) NXSTR(x)   /* stringize after expansion */

      asm(" move.l #" STR(OPEN_OBJECT) ",D0"),4 );


----- Explanation ----

   Aha! I see it now.  When you do the two-level evaluation in the style:

#define OPEN_OBJECT 12
#define NXSTR(x) #x    /* no-expansion stringize */
#define STR(x) NXSTR(x)   /* stringize after expansion */

      asm(" move.l #" STR(OPEN_OBJECT) ",D0",4 );

the result is that the original text of:

      asm(" move.l #" STR(OPEN_OBJECT) ",D0",4 );

gets replaced after one level of evaluation with:

      asm(" move.l #" NXSTR(12) ",D0",4 );

which is then re-evaluated to give:

      asm(" move.l #" "12" ",D0",4 );

which the c-compile then treats as:

      asm(" move.l #12,D0",4 );

and passes the assembler:

      " move.l #12,D0"

and that is what I want!

  ...Paul
-- 
Paul Stachour          SCTC, 1210 W. County Rd E, Suite 100           
stachour at sctc.com          Arden Hills, MN  55112
                             [1]-(612) 482-7467



More information about the Comp.lang.c mailing list