Need help with quoting and the CPP

Jennifer H. Zemsky jhz at cunixa.cc.columbia.edu
Mon Apr 22 11:42:37 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).  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";
>
from k&r (2nd ed), 4.11.2:
if ... a parameter is preceded by a # in the replacement text, the combination
will be expanded into a quoted string with the parameter replace by the actual
argument. This can be combined with string concatenation to make, for
example, a debugging print macro:

#define dprint(expr) printf(#expr " = %g\n",expr)
When this is invoked, as in

dprint (x/y);

the macro is expanded into
printf("x/y" " = %g\n", x/y);
and the strings are concatenated ...
printf("x/y = %g\n",x/y);

(end quote)
you could try
#define QUOTE(x) #x

and see if it works.
(note: the extra information was to head off some other questions.)

>Thanks in advance.

no prob.

--hymie
jhz at cunixa.cc.columbia.edu
-------------------------------------------------------------------------------
note: the above information, knowledge, etc. is offered only on an
as-is basis.  the author takes no responsibility for innacuracy or for
problems caused by implementing said information.  veracity is not
guaranteed.  corrections welcome.
-------------------------------------------------------------------------------






More information about the Comp.lang.c mailing list