#define toString(x) #x这个宏就可以将所有的数字,包括int型、long型和double型转换为相对应的字符串。关于这种类似的用法还有:
#define makechar(x) #@x
a = makechar(b);
#define stringer( x ) printf( #x "\n" )
void main()
{
stringer( In quotes in the printf function call\n );
stringer( "In quotes when printed to the screen"\n );
stringer( "This: \" prints an escaped double quote" );
}
//预处理时将会产生如下代码。
void main()
{
printf( "In quotes in the printf function call\n" "\n" );
printf( "\"In quotes when printed to the screen\"\n" "\n" );
printf( "\"This: \\\" prints an escaped double quote\"" "\n" );
}
运行结果:
In quotes in the printf function call
"In quotes when printed to the screen"
"This: \" prints an escaped double quotation mark"
这种用法可以省去转义字符(\),很方便代码的编写。
关于#的用法还有很多,希望有兴趣的读者能够留言,我们一起讨论。
编辑特别推荐: