宏定义

 • 宏定义。 #define 定义一个宏, #undef 删除一个宏定义。

 

------------------------------------------------------------------------------------

 >> 简单的宏

     #define  标识符   替换列表

 

    #define  TRUE  1
   
#define  FALSE 0
   
#define  CR    '\r'

 

------------------------------------------------------------------------------------

 >> 带参数的宏

      #define  标识符(x1, x2, x3, ...)   替换列表

 

    #define  MAX(x,y)  ((x)>(y)?(x):(y))
   
#define  IS_EVEN(n)  ((n)%2==0)

 

      如出现如下代码: 

   a  =  MAX(m + n,k + j);
   
if  (IS_EVEN(i)) i ++ ;

 

      则替换为:

   a  =  ((m + n) > (k + j) ? (m + n):(k + j));
   
if  ((i) % 2 == 0 ) i ++ ;

 

------------------------------------------------------------------------------------

>> # 和 ##

     宏定义中的# 和 ##将在预处理时执行

     # :

    #define  PRINT_INT(x)  printf(#x " = %d\n", x)

   PRINT_INT(n
/ m);
   
//  会变成如下格式
   printf( " n/m "   "  = %d\n " , n / m);
   
//  等价于
   printf( " n/m = %d\n " , n / m);

 

     ##:

    #define  M_ID(n) i##n

   
int  M_ID( 1 ), M_ID( 2 ), M_ID( 3 );
   
//  变为
    int  i1, i2, i3;

 

------------------------------------------------------------------------------------

 

你可能感兴趣的:(宏定义)