tags:

views:

92

answers:

2

Is it possible to stringify a character in a preprocessor macro without it including the (')s

example:

#define S(name, chr)  const char * name = #name chr

usage:

S(hello, 'W'); //should expand to 'const char * hello = "helloW"

Thanks a bunch!, Andrew

+9  A: 

You don't need to, because in C adjacent string constants are merged.

ie.

const char *hello = "hello" "W";

is equivalent to

const char *hello = "helloW";

so your current macro is fine - just call it like so:

S(hello, "W");
caf
+1 Side note - it's C preprocessor who merges adjacent string literals
qrdl
+1. @grdl: you are wrong. Check with running cpp on this macro, it does not merge the adjacent literals, it's the compiler that does.
Andrew Y
+9  A: 

Here are three ways. None use a single-quoted char, though:

#include <iostream>

#define S1(x, y) (#x #y)
#define S2(x, y) (#x y)
#define S3(x, y) (x y)

int main(void)
{
    std::cout << S1(hello, W) << std::endl;
    std::cout << S2(hello, "W") << std::endl;
    std::cout << S3("hello", "W") << std::endl;
};

All output:

helloW

GMan