日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

c语言宏嵌套和展开规则

發布時間:2023/11/27 生活经验 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 c语言宏嵌套和展开规则 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

基本原則:

在展開當前宏函數時,如果形參有#或##則不進行宏參數的展開,否則先展開宏參數,再展開當前宏。

#是在定義兩邊加上雙引號

#define _TOSTR(s) #sprintf(_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##yprintf(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##yprintf(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! ");

總結

以上是生活随笔為你收集整理的c语言宏嵌套和展开规则的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。