You _CAN_ "stringize" tokens in (some) K&R pre-processors
Shaun Case
shaunc at gold.gvg.tek.com
Wed Apr 3 13:29:03 AEST 1991
In article <323 at secola.Columbia.NCR.COM> jmartin at secola.Columbia.NCR.COM (John V. Martinez) writes:
>
> I seem to recall a thread of discussion a couple of weeks ago
>about how to "stringize" a token using the K&R C preprocessor (ANSI C
>provides the # operator to do this.) Anyway, as I recall, the concensus
>was that it couldn't be done. (At least, no one submitted a method, as
>far as I know...)
>
> Well, now I had to try (the Mt. Everest principle, you know,)
>so I locked myself in my laboratory with a six-pack of Mountain Dew and
>a cold pizza, until I discovered a magic spell that (sort of) did the
>trick:
>
>#define quote "
>#define stringize(A) quote A"
>
>this is _almost_ right; stringize(foobar) --(magic)--> " foobar"
>
Hey, I like that!
It gave me an idea, too... I tried the following, and it worked under
some random version of Ultrix:
--- cut here---
#include <stdio.h>
#define quote "
#define stringize(a) (quote a"+1)
main()
{
printf("[%s]\n",stringize(foo));
}
--- cut here ---
Is there some reason why this is a bad idea?
// Shaun //
More information about the Comp.lang.c
mailing list