当前所在位置:珠峰网资料 >> 计算机 >> 计算机等级考试 >> 正文
C++如何将数字(包括double型)转换为字符串
发布时间:2010/7/22 11:14:21 来源:城市学习网 编辑:ziteng
  #define toString(x) #x这个宏就可以将所有的数字,包括int型、long型和double型转换为相对应的字符串。关于这种类似的用法还有:
  #define makechar(x)    #@x
  a = makechar(b);
  这个结果就相当于a='b'。来源:www.examda.com
  #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"
  这种用法可以省去转义字符(\),很方便代码的编写。
  关于#的用法还有很多,希望有兴趣的读者能够留言,我们一起讨论。
广告合作:400-664-0084 全国热线:400-664-0084
Copyright 2010 - 2017 www.my8848.com 珠峰网 粤ICP备15066211号
珠峰网 版权所有 All Rights Reserved