C语言再学习

一、宏定义

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

发布了255 篇原创文章 · 获赞 39 · 访问量 35万+

猜你喜欢

转载自blog.csdn.net/kangguang/article/details/80423272
今日推荐