码迷,mamicode.com
首页 > 编程语言 > 详细

C/C++宏定义中#与##区别

时间:2017-04-17 12:45:34      阅读:190      评论:0      收藏:0      [点我收藏+]

标签:区别   表示   字符串   char   div   ++   orm   ace   mat   

 

 1     // #表示:对应变量字符串化  
 2     // ##表示:把宏参数名与宏定义代码序列中的标识符连接在一起,形成一个新的标识符  
 3     #include <stdio.h>  
 4     #define trace(x, format) printf(#x " = %" #format "\n", x)  
 5     #define trace2(i) trace(x##i, d)  
 6       
 7     int main()  
 8     {  
 9         int i=1;  
10         char *s="three";  
11         float x=2.0;  
12       
13         trace(i, d);                // 相当于 printf("i = %d\n", x) ,输出结果:i = 1
14         trace(x, f);                // 相当于 printf("x = %f\n", x) ,输出结果:x = 2.000000
15         trace(s, s);                // 相当于 printf("s = %s\n", x) ,输出结果:s = three
16       
17         int x1=1, x2=2, x3=3;  
18         trace2(1);              // 相当于 trace(x1, d),输出结果:x1 = 1  
19         trace2(2);              // 相当于 trace(x2, d),输出结果:x2 = 2 
20         trace2(3);              // 相当于 trace(x3, d),输出结果:x3 = 3
21       
22         return 0;  
23     } 

 

C/C++宏定义中#与##区别

标签:区别   表示   字符串   char   div   ++   orm   ace   mat   

原文地址:http://www.cnblogs.com/deityde1127/p/6722229.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!