Need help with quoting and the CPP

Christopher R Volpe volpe at camelback.crd.ge.com
Mon Apr 22 22:34:37 AEST 1991


In article <1991Apr22.014237.16836 at cunixf.cc.columbia.edu>,
jhz at cunixa.cc.columbia.edu (Jennifer H. Zemsky) writes:
|>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).  For example if
|>>STRING is defined as:
|>>
|>>#define STRING test string
|>>
|>>Is there any way to define a macro "QUOTE" so that:
|>>
|>>char* s = QUOTE(STRING);
|>>
|>>while produce:
|>>
|>>char *s = "test string";
|>>
|>you could try
|>#define QUOTE(x) #x
|>

Won't work. Stringizing takes place on the unexpanded argument. Using your
QUOTE,
  char *s = QUOTE(STRING);
becomes
  char *s = "STRING";

which is not what he wanted.

You must define an auxialliary macro which allows expansion to occur
before stringizing. 

#define QUOTE(x) QUOTE_AUX(x)
#define QUOTE_AUX(x) #x

Now, QUOTE(STRING) produces "test string"

-Chris                                                  
==================
Chris Volpe
G.E. Corporate R&D
volpecr at crd.ge.com



More information about the Comp.lang.c mailing list