C++如何将数字(包括double型)转换为字符串

2014-10-23 15:30:04 · 作者: · 浏览: 63


  #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"


  这种用法可以省去转义字符(\),很方便代码的编写。


  关于#的用法还有很多,希望有兴趣的读者能够留言,我们一起讨论。


  编辑特别推荐: