C语言宏定义时#(井号)和##(双井号)的用法

#在英语里面叫做 pound

在C语言的宏定义中,一个#表示字符串化;两个#代表concatenate

在宏定义中,#的功能是将其后面的宏参数进行字符串化操作(Stringfication),即无论后面的宏变量是字符串还是常量,都当成字符来处理。

而## 被称为连接符(concatenator),用来将两个Token连接为一个Token。注意这里连接的对象是Token就行,而不一定是宏的变量。

 

举例如下:

[cpp]  view plain copy
  1. #include <iostream>  
  2. void quit_command(){  
  3.     printf("I am quit command\n");  
  4. }     
  5. void help_command(){  
  6.     printf("I am help command\n");  
  7. }     
  8. struct command  
  9. {  
  10.     char * name;  
  11.     void (*function) (void);  
  12. };    
  13. #define COMMAND(NAME) {#NAME,NAME##_command}  
  14. #define PRINT(NAME) printf("token"#NAME"=%d\n", token##NAME)  
  15. main(){  
  16.     int token9=9;  
  17.     PRINT(9);  
  18.     struct command commands[] = {  
  19.         COMMAND(quit),  
  20.         COMMAND(help),  
  21.     };    
  22.     commands[0].function();  
  23. }  


 

得到的结果是:

 

token9=9
I am quit command

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值