以下代码如何正确编译,
#include <stdio.h>
#define stringer( x ) printf_s( #x "\n" )
int main() {
stringer( "In quotes when printed to the screen" );
}
它不应该扩展到
printf_s(""In quotes when printed to the screen""\n");
这是一个错误,因为 printf_s 中有嵌套的双引号?
不,#
运算符专门处理字符串文字。它必须在传递给它的字符串文字中\
转义每个。"
正确的展开是:
printf_s( "\"In quotes when printed to the screen\"" "\n" );
不,它已扩展为
printf_s("\"In quotes when printed to the screen\"" "\n");
最终将是
printf_s("\"In quotes when printed to the screen\"\n");
并且应该打印
"In quotes when printed to the screen"
在 C 中,相邻的字符串文字被连接起来:
相邻的字符串文字在编译时连接;这允许将长字符串拆分为多行,并且还允许在编译时将由 C 预处理器定义和宏生成的字符串文字附加到字符串: