Re: Numeric constants as strings
- From
- Andy Parkins <andyparkins@gmail.com>
- Date
- Feb 19, 2007, 10:54 UTC
- Message-ID
- <200702191055.00203.andyparkins@gmail.com>
- In-Reply-To
- <vpq1wkm31q4.fsf@olympe.imag.fr>
On Monday 2007 February 19 10:01, Matthieu Moy wrote:
Show 7 quoted lines
> "Shawn O. Pearce" <spearce@spearce.org> 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 andyparkins@gmail.com