[comp.lang.c] You _CAN_ "stringize" tokens in

jmartin@secola.Columbia.NCR.COM (John Martinez) (04/02/91)

	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"

	The next problem is to remove the leading space (if you don't
put a space between 'quote' and 'A' in the macro, it sees 'quoteA' as a
token, which doesn't resolve to anything interesting..)

Another slice of cold pizza later.....
-(POOF)-
*********
static char *global_char_ptr;  /* 'static' is just a personal preference. */
#define quote "
#define stringize(a) (global_char_ptr=quote a",++global_char_ptr)

main()
{
int foo=5;

printf("%s = %d\n",stringize(foo),foo);
}
*********
_my_ system's preprocessor converts the printf line to:

printf("%s = %d\n",(global_char_ptr=" foo",++global_char_ptr),foo);

which outputs:
foo = 5

Voila!

NOTE: I'm sure there are lots of netters who:
a) already knew about this
b) saw a solution just like it in a posting I never saw 
	_or_
c) think the whole discussion is dumb since "the whole world should have
bought an ANSI C compiler 4.7 microseconds after the standards were
signed, no matter what the cost, and if you can't find a compiler you
can just write your own in 2 hours, otherwise you're not a real programmer"

	I have a established a special account, devnull@TheAbyss for
you to reply by mail. Seriously - I posted this because I thought it
would be of use to someone, and I am pretty darn happy about it, so
don't rain on my parade, ok?


---
John V.Martinez                   "Yeah, I got a college degree, but somehow I
NCR Network Products Division      managed to learn something in spite of it."
jmartin@secola.Columbia.NCR.COM
(803)772-8854 vplus:633-8854

volpe@camelback.crd.ge.com (Christopher R Volpe) (04/02/91)

I thought just about all K&R1 based compilers expanded formal arguments
within quotes, so the following would work:

#define STRINGIZE(x) "x"

STRINGIZE(foo) ==> "foo"

At least it works on SunOS cc.

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

gwyn@smoke.brl.mil (Doug Gwyn) (04/03/91)

In article <18148@crdgw1.crd.ge.com> volpe@camelback.crd.ge.com (Christopher R Volpe) writes:
>I thought just about all K&R1 based compilers expanded formal arguments
>within quotes, so the following would work:
>#define STRINGIZE(x) "x"

No, in fact K&R1 says that the argument must not be replaced within
string literals in the replacement string.

The behavior you cite is a long-standing bug in the Reiser CPP that
was shipped with UNIX for many years.

shaunc@gold.gvg.tek.com (Shaun Case) (04/03/91)

In article <323@secola.Columbia.NCR.COM> jmartin@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 //

cnwietho@immd4.informatik.uni-erlangen.de (Carsten Wiethoff) (04/04/91)

I prefer

#define QUOTE(x) "x
#define STRINGINIZE(x) QUOTE(x)"

which does not have the problem with spaces before x.

Carsten Wiethoff		| cnwietho@immd4.informatik.uni-erlangen.de

barnettr@snaggle.rtp.dg.com (Richard Barnette) (04/04/91)

In article <323@secola.Columbia.NCR.COM> jmartin@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...)
>

    Some preprocessors will accept

#define stringize(A) "A"

GNU C will do this if you say -traditional.  The AT&T unix compilers
tend to do this (but I reckon it depends on your system).
Richard Barnette      | Data General Corporation | obligatory (in)famous quote:
(919) 248-6225        | RTP, NC  27709	         |  You wascal wabbit!
Commercial Languages  | 62 T.W. Alexander Drive	 |  Wandering wizards won't
barnettr@dg-rtp.dg.com				 |  win! - /usr/lib/sendmail

timr@gssc.UUCP (Tim Roberts) (04/05/91)

In article <1991Apr3.193750.14282@informatik.uni-erlangen.de> cnwietho@immd4.informatik.uni-erlangen.de (Carsten Wiethoff) writes:
>I prefer
>
>#define QUOTE(x) "x
>#define STRINGINIZE(x) QUOTE(x)"
>
>which does not have the problem with spaces before x.

If that construct works for some preprocessor, then wouldn't the much simpler

	#define STRINGINIZE(x)	"x"

also work?

-- 
timr@gssc.gss.com	Tim N Roberts, CCP	Graphic Software Systems
						Beaverton, OR

This is a very long palindrome. .emordnilap gnol yrev a si sihT