Subject: Re: Numeric constants as strings

From: Andy Parkins <[email-masked]>
Date: Mon, 19 Feb 2007 10:54:58 +0000

On Monday 2007 February 19 10:01, Matthieu Moy wrote:
> "Shawn O. Pearce" <[email-masked]> writes:
> > I did not even realize that was legal C...  Now if the 40 was in
> > quotes (e.g. "40") then the concatenate rule would apply and we
> > would get a nice argument to printf.
>
> I suppose the solution is to use #HASH_WIDTH_ASCII to tell the
> preprocessor to put the quotes around HASH_WIDTH_ASCII.

I'm afraid that only works when you're token pasting parameters in a #define 
macro.

For example:

 #define macro(x) "foo" #x "baz"

Then, macro(bar) expands to "foo" "bar" "baz".  However, the following does 
not work:

 #define BAR bar
 #define macro "foo" #BAR "baz"
 
This is because BAR is not a macro parameter.  I've also tried it indirectly:

 #define BAR bar
 #define MAKESTRING(x) #x
 #define macro "foo" MAKESTRING(BAR) "baz"

But this expands to "foo" "BAR" "baz".  Also wrong.  Equally, using # anywhere 
but during a #define doesn't work, so I can't simply write

 printf( "%-" #HASH_WIDTH_ASCII "s", string );

Woe is me. :-(

Andy
-- 
Dr Andy Parkins, M Eng (hons), MIEE
[email-masked]