如何通过C预处理器打印磅/哈希?

tue*_*ist 8 c c-preprocessor

我需要帮助做以下事情:

预处理器宏标签(x)应输出"#x",例如,

#define label(x) ...
Run Code Online (Sandbox Code Playgroud)

如果我调用label(aname),输出应为"#aname"(不带引号)

我知道,以下尝试都是错误.

#define label(x) #x   // leads to "x"
#define label(x) \#x  // is \"x"
#define label(x) "#x" // is "#x" (but not the content of x") "#otto"
Run Code Online (Sandbox Code Playgroud)

它可能存在一种逃脱#(磅),但我不知道,如何逃脱......

编辑:我运行"gcc -E test -o test.html"来获取输出.重点是:如何仅使用预处理器的功能使用makro打印哈希标记(#)?

tue*_*ist 13

答案是:

#define hash #
#define f(x) x
#define label(a) f(hash)a
Run Code Online (Sandbox Code Playgroud)

然后

label(foobar)
Run Code Online (Sandbox Code Playgroud)

创建

#foobar
Run Code Online (Sandbox Code Playgroud)

我是在你们所有人的帮助下找到的,尤其是冬季的.非常感谢!

(使用gcc 4.3.3)