c/c++中#号的用法

转载:http://blog.sina.com.cn/s/blog_4cf015f201009o1d.html

第一种情况作为Stringizing Operator(#),暂叫字符串化吧。

 #define stringer( x ) printf( #x "\n" ) 

int 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" ); 

}

如此的定义在进行处理时将被转换为如下代码: 

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


第二种情况作为Charizing Operator(#@),就叫字符化吧。
例子:
#define makechar(x) #@x
causes the statement
a = makechar(b);
这条语句将被扩展为

a = 'b';

注意单引号字符不能使用这样的字符化。

第三种情况作为

Token-Pasting Opertor(##),叫做连接符吧,大概是这样的意思。

例子:#define paster( n ) printf( "token" #n " = %d", token##n ) int token9 = 9;

如果一个数字作为此宏的参数调用的话,
paster( 9 );
宏被展开为
printf( "token" "9" " = %d", token9 );

进而成为这样的语句:printf( "token9 = %d", token9 );



参考链接:http://blog.sina.com.cn/s/blog_4cf015f201009o1d.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值