I need help doing the following:
a preprocessor macro label(x) shall output \"#x\", e.g.,
#define label(x) ...
if I call label(anam
String literals in C will be concatenated, so you can do
#define label(x) "#" #x
I don't think it's possible without string concatenation (ie without invoking the C compiler as you want to do):
You can do some fancy stuff with additional levels of indirection and I even got the preprocessor to generate the desired output via
#define hash #
#define quote(x) #x
#define in_between(c, d) quote(c ## d)
#define join(c, d) in_between(c, d)
#define label(x) join(hash, x)
label(foo)
The problem is it will also generate an error message as in_between()
expands to #foo
, which is not an valid preprocessor token. I don't see any way around this.
My advise would be to choose the right tool for the job: switch to another macro language like m4 or even ML/I if you feel adventurous or use a scripting language like PHP or Perl. GPP seems nice as well and might be a better fit.