C語言再學習

清雨未盡時發表於2018-05-23

一、巨集定義

1. #define area(x)   printf("The area of  x is %d\n",(x * x));

 area(2+5);

列印資訊:The area of  x is 17

巨集展開 為 2 + 5 * 2 + 5


所以應該 改為:

#define area(x)   printf("The area of  x is %d\n",((x) * (x)));

列印資訊:The area of  x is 49


2.# 引數直接列印

#define areaX(x)   printf("The area of  "#x" is %d\n",((x) * (x)));

areaX(2+5)

列印資訊:The area of  2+5 is 49


3.## 拼接

#define NAME(n)    a##n

#define PRINT(n)     printf("a"#n" = %d\n",a##n);

 int NAME(1) = 5;
 int NAME(2) = 15;
 PRINT(1);
 PRINT(2);
        

 列印資訊:

   a1 = 5
   a2 = 15

相關文章