C语言中的宏#和##
#的功能是将其后面的宏参数进行字符串化操作(Stringizing),简单说就是在对它所引用的宏变量通过替换后在其左右各加上一个双引号。
Causes the corresponding actual argument to be enclosed in double quotation marks
##被称为连接符(concatenator),用来将两个Token连接为一个Token。注意这里连接的对象是Token就行,而不一定是宏的变量。
Allows tokens used as actual arguments to be concatenated to form other tokens
假设程序中已经定义了这样一个带参数的宏:
#define paster( n ) printf( "token" #n " = %d", token##n )
同时又定义了一个整形变量:
int token9 = 9;
现在在主程序中以下面的方式调用这个宏:
paster( 9 );
那么在编译时,上面的这句话被扩展为:
printf( "token" "9" " = %d", token9 );
reference:
浙公网安备 33010602011771号