C/C++ 宏定义中#、##、#@的区别

#表示:对应变量字符串化  

##表示:把宏参数名与宏定义代码序列中的标识符连接在一起,形成一个新的标识符

连接符#@:它将单字符标记符变换为单字符,即加单引号。例如:

#define B(x) #@x

 则B(a)即'a',B(1)即'1',但B(abc)却不甚有效。

[cpp] view plain copy
 
 print?在CODE上查看代码片派生到我的代码片
  1. #include <stdio.h>    
  2. #define trace(x, format) printf(#x " = %" #format " ", x)    
  3. #define trace2(i) trace(x##i, d)   
  4.   
  5. int _tmain(int argc, _TCHAR* argv[])  
  6. {  
  7.     int i = 1;  
  8.     char *s = "three";    
  9.     float x = 2.0;  
  10.   
  11.     trace(i, d);                // 相当于 printf("x = %d ", x)  
  12.     trace(x, f);                // 相当于 printf("x = %f ", x)  
  13.     trace(s, s);                // 相当于 printf("x = %s ", x)  
  14.   
  15.     int x1 = 1, x2 = 2, x3 = 3;    
  16.     trace2(1);                  // 相当于 trace(x1, d)  
  17.     trace2(2);                  // 相当于 trace(x2, d)  
  18.     trace2(3);                  // 相当于 trace(x3, d)  
  19.   
  20.     return 0;  
  21. }  

输出结果:

i = 1
x = 2.000000
s = three
x1 = 1
x2 = 2
x3 = 3

原文地址:https://www.cnblogs.com/fnlingnzb-learner/p/6823575.html