Stringizing escape character tokens, std. or not?
- From: James Antill <james-netnews@xxxxxxx>
- Date: Sat, 23 Jun 2007 05:40:07 -0000
The short form of my question is, is this snippet valid C:
#define FOO(x) "1234" #x "5678"
const char *mystr1 = FOO(\n);
const char *mystr2 = FOO(\x20);
....as far as I can tell, it is.
6.10.3.2 implies that it "does the right thing" as long as the input is
a pre-processing token and the output is a valid character string literal.
\n is two valid pre-processing tokens due to the text at 6.4#3 and
6.4.6, and that is stringized and then treated the same way as "\n" etc.
Some background:
The explanation of why I want to do this, is that I want to create
constant strings with a length at the beginning and the nicest approach
that I'm leaning towards is:
#define XSTR(x, y) #x y
const char *mystr = XSTR(\4, "1234");
....which seems nicer than the only real alternative of:
#define XSTR(x, y) x y
const char *mystr = XSTR("\4", "1234");
....but if anyone knows of a nicer way of doing the above, that would be
even better :).
--
James Antill -- james@xxxxxxx
C String APIs use too much memory? ustr: length, ref count, size and
read-only/fixed. Ave. 55% overhead over strdup(), for 0-20B strings
http://www.and.org/ustr/
.
- Follow-Ups:
- Re: Stringizing escape character tokens, std. or not?
- From: Douglas A. Gwyn
- Re: Stringizing escape character tokens, std. or not?
- Prev by Date: Re: Defeating the type aliasing rules
- Next by Date: Issue with sscanf
- Previous by thread: The null directive (6.10.7 of n1124)
- Next by thread: Re: Stringizing escape character tokens, std. or not?
- Index(es):
Relevant Pages
|