今天跟一个在腾讯工作的同学聊天了,他问我如何将一个数转换为一个字符串,我跟他说是这样的:
char buffer[10];
_itoa(i, buffer, 10);
_itoa(i, buffer, 10);
#define toString(x) #x
#define makechar(x) #@x
a = makechar(b);
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"
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"
关于#的用法还有很多,希望有兴趣的读者能够留言,我们一起讨论。
本文出自 “C++技术” 博客,请务必保留此出处http://panpan.blog.51cto.com/489034/102813
实践:
#define toString(x) (#x)
#define stringer(x) (printf(#x"\n"))
#define mynameandbirthday(x) ("hekexin"#x)
int main()
{
std::string str(toString(1000));
std::cout<<str<<std::endl;
stringer(hello world);
stringer(1234fff5455);
std::cout<<mynameandbirthday(19841102)<<std::endl;
std::cout<<mynameandbirthday(19841223)<<std::endl;
return 0;
}