c语言宏嵌套和展开规则

基本原则:

在展开当前宏函数时,如果形参有#或##则不进行宏参数的展开,否则先展开宏参数,再展开当前宏。

#是在定义两边加上双引号

#define _TOSTR(s) #s

printf(_TOSTR(test ABC))
printf(_TOSTR("test ABC"));
printf(_TOSTR(_TOSTR(test ABC)));

==================
预编译结果:
printf("test ABC")
printf("\"test ABC\"");
printf("_TOSTR(test ABC)");

##是原样代入

 

#define HI_BOY HiBoy!
#define __ORIGINAL(s) s
#define _TOSTR(s) #s
#define STR_CONCAT(x, y) x##y

printf(HI_BOY);
printf(__ORIGINAL(HI_BOY));
printf(_TOSTR(HI_BOY));
printf(_TOSTR(__ORIGINAL(HI_BOY)));
printf(STR_CONCAT(HI_BOY, HI_BOY ));
printf(STR_CONCAT(_TOSTR(HI_BOY), _TOSTR(HI_BOY) ));
===========================================================
预处理后的结果:
printf(HiBoy!);
printf(HiBoy!);
printf("HI_BOY");
printf("__ORIGINAL(HI_BOY)");
printf(HI_BOYHI_BOY);
printf("HI_BOY" "HI_BOY");

连续的两个双引号会被忽略掉

以下写法结果一样:

printf("this is a test sentence. are you ok?\n");
printf("this is a test ""sentence."" are you ok?\n");
printf("this is a test sentence."   " are you ok?\n");
printf("this is a"           " test ""sentence. are"" you ok?\n");

==============
this is a test sentence. are you ok?
this is a test sentence. are you ok?
this is a test sentence. are you ok?
this is a test sentence. are you ok?

嵌套

#define  QUOTATION "
#define HI_BOY HiBoy!
#define __ORIGINAL(s) s
#define _TOSTR(s) #s
#define STR_CONCAT(x, y) x##y

printf(HI_BOY);
printf(__ORIGINAL(HI_BOY));
printf(_TOSTR(HI_BOY));
printf(_TOSTR(__ORIGINAL(HI_BOY)));
printf(STR_CONCAT(HI_BOY, HI_BOY ));
printf(STR_CONCAT( _TOSTR(HI_BOY), _TOSTR(HI_BOY) ));
printf(                  STR_CONCAT( __ORIGINAL(HI_BOY), __ORIGINAL(HI_BOY) ));
printf(           _TOSTR(STR_CONCAT( __ORIGINAL(HI_BOY), __ORIGINAL(HI_BOY) )));
printf(_TOSTR(__ORIGINAL(STR_CONCAT( __ORIGINAL(HI_BOY), __ORIGINAL(HI_BOY) ))));
printf(           ""(STR_CONCAT( __ORIGINAL(HI_BOY), __ORIGINAL(HI_BOY) ))"");
printf(_TOSTR(__ORIGINAL(STR_CONCAT( __ORIGINAL(HI_BOY), __ORIGINAL(HI_BOY) ))));
printf(           QUOTATION(STR_CONCAT( __ORIGINAL(HI_BOY), __ORIGINAL(HI_BOY) ))QUOTATION);
printf(           QUOTATION STR_CONCAT( __ORIGINAL(HI_BOY), __ORIGINAL(HI_BOY) )QUOTATION);


==================================================
预编译结果:
printf(HiBoy!);
printf(HiBoy!);
printf("HI_BOY");
printf("__ORIGINAL(HI_BOY)");
printf(HI_BOYHI_BOY);
printf("HI_BOY" "HI_BOY");
printf(                  HiBoy!HiBoy!);
printf(           "STR_CONCAT( __ORIGINAL(HI_BOY), __ORIGINAL(HI_BOY) )");
printf("__ORIGINAL(STR_CONCAT( __ORIGINAL(HI_BOY), __ORIGINAL(HI_BOY) ))");
printf(           ""(HiBoy!HiBoy!)"");
printf("__ORIGINAL(STR_CONCAT( __ORIGINAL(HI_BOY), __ORIGINAL(HI_BOY) ))");
printf(           "(HiBoy!HiBoy!)");
printf(           " HiBoy!HiBoy! ");
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值