stringify with embedded quotes

R

Randy Kobes

I have a couple of macros:

#define QUOTE_ME_HELPER(foo) #foo
#define QUOTE_ME(foo) QUOTE_ME_HELPER(foo)

and was wanting to use them as, for example,
const char bar[] = QUOTE_ME("domain=example.com");
I'm having a problem with embedded quotes though, using
VC++ 6 (SP5). This
const char bar[] = QUOTE_ME("domain=\"example.com");
expands to
const char bar[] = "\"domain=\\\"example.com\"";
which is OK, but
const char bar[] = QUOTE_ME("domain=\"example.com\"");
expands to
const char bar[] = "\"domain=\\\"example.com\\"\"";
which leads to an illegal escape sequence error. Both
examples work OK with gcc.

Is the above expected to work with VC++ 6? If it
"should" work, yet doesn't, can anyone suggest a
workaround, hopefully at the level of a macro?
Thanks very much.
 
K

Kevin Bracey

In message <[email protected]>
Randy Kobes said:
I have a couple of macros:

#define QUOTE_ME_HELPER(foo) #foo
#define QUOTE_ME(foo) QUOTE_ME_HELPER(foo)

const char bar[] = QUOTE_ME("domain=\"example.com\"");
expands to
const char bar[] = "\"domain=\\\"example.com\\"\"";
which leads to an illegal escape sequence error. Both
examples work OK with gcc.

Is the above expected to work with VC++ 6?

It should work. The rule is very simple:

6.10.3.2p2:

... a \ character is inserted before each " and \ character of
a character constant or string literal (including the delimiting
" characters), ...

Can't think why the compiler would convert the first \" correctly to \\\" but
then muff up the second one. Can't think of a workaround either.
 
S

Sisyphus

Randy said:
I have a couple of macros:

#define QUOTE_ME_HELPER(foo) #foo
#define QUOTE_ME(foo) QUOTE_ME_HELPER(foo)

and was wanting to use them as, for example,
const char bar[] = QUOTE_ME("domain=example.com");
I'm having a problem with embedded quotes though, using
VC++ 6 (SP5). This
const char bar[] = QUOTE_ME("domain=\"example.com");
expands to
const char bar[] = "\"domain=\\\"example.com\"";
which is OK, but
const char bar[] = QUOTE_ME("domain=\"example.com\"");
expands to
const char bar[] = "\"domain=\\\"example.com\\"\"";
which leads to an illegal escape sequence error. Both
examples work OK with gcc.

Is the above expected to work with VC++ 6? If it
"should" work, yet doesn't, can anyone suggest a
workaround, hopefully at the level of a macro?
Thanks very much.

Seems to have been fixed with MSVC++ 7.0 - though that still doesn't
help with a workaround (unless you consider using MSVC++ 7.0 a
workaround :)

But the whole thing seems buggy to me on every compiler I can access
when backslashes or double quotes are involved.

As an example, the following won't compile on gcc (linux), gcc (Win32),
VC++ 6.0 or VC++ 7.0 - though, admittedly, faik, there may be a good
reason for the failure:

#include <stdio.h>
#define QUOTE_ME(foo) printf(#foo);

int main(void) {
QUOTE_ME(\")
return 0;
}

I thought that should be the same as either printf("\"") or perhaps
printf("\\\"") - but it simply won't compile. Is my expectation misguided ?

Cheers,
Rob
 
C

Chris Torek

(I was saving some stuff to reply to, when my newsreader and/or server
glitched. Found this one...)

... the following won't compile on gcc (linux), gcc (Win32),
VC++ 6.0 or VC++ 7.0 - though, admittedly, faik, there may be a good
reason for the failure:

#include <stdio.h>
#define QUOTE_ME(foo) printf(#foo);

int main(void) {
QUOTE_ME(\")
return 0;
}

I thought that should be the same as either printf("\"") or perhaps
printf("\\\"") - but it simply won't compile. Is my expectation misguided ?

I believe it is (misguided): the preprocessor is only required
to work with valid "pp-token"s, and \" is not a valid pp-token.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,832
Latest member
GlennSmall

Latest Threads

Top