git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: Numeric constants as strings

From
APAndy 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
Previous: Matthieu MoyNext: Andy Parkins
Message 5 of 11 in “Numeric constants as strings”
  1. Andy ParkinsFeb 19, 2007
  2. Junio C HamanoFeb 19, 2007
  3. Shawn O. PearceFeb 19, 2007
  4. Matthieu MoyFeb 19, 2007
  5. Andy ParkinsFeb 19, 2007
  6. Andy ParkinsFeb 19, 2007
  7. Junio C HamanoFeb 19, 2007
  8. Andy ParkinsFeb 19, 2007
  9. Mark WoodingFeb 19, 2007
  10. Andy ParkinsFeb 19, 2007
  11. Jakub NarebskiFeb 20, 2007

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.