Stringizing escape character tokens, std. or not?




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/
.



Relevant Pages

  • Re: Implementing strstr
    ... that string doesn't appear at all). ... const char* strstr(const char* haystack, const char* needle) ...
    (comp.programming)
  • curses mouse cut/paste on solaris2.9, hangs on select(). Works fine on aix and linux
    ... The program works fine on solaris, if the user inputs the string from ... const char *defaultText = NULL; ... void tstWprintw; ... int main ...
    (comp.unix.solaris)
  • Pinvoke reading files, returning char * values.
    ... const char* strInputFileName, ... back a string. ... public static extern bool GetXMLString(string inputFileName, ... StringBuilder output, ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Whats wrong with the string?
    ... Then it is better to use "const char *" so that the ... >> putc() is a function to put a char on the serial console. ... the str could point beyond the end of your string. ... characters to characters. ...
    (comp.os.linux.embedded)
  • Re: Destroy a string
    ... "rossum" wrote in message ... const char overwriteChar = 'X'; ... The string is still on the heap, ... completely secure, all that is possible is to make the attacker's job ...
    (microsoft.public.dotnet.languages.csharp)