在C是不正常可以使用'的printf字符串.但是,我的文字充满双引号",我需要将它们全部转义为
printf("This is \"test\" for another \"text\"");
Run Code Online (Sandbox Code Playgroud)
是否有可能printf在没有逃脱的情况下".我的意思是使用另一个字符来包装字符串.
不推荐,但您可以使用宏:
#include <stdio.h>
#define S(x) #x
int main() {
printf(S(This "is" a string (with nesting)!\n));
}
Run Code Online (Sandbox Code Playgroud)
这打印
This "is" a string (with nesting)!
Run Code Online (Sandbox Code Playgroud)
现在分隔符是平衡的()字符.但是,要逃避单个),"或'字符,你必须写出类似的东西S(right paren: ) ")" S(!\n),这是非常难看的.建议不要将此技术用于编写可维护代码.