C中的宏的使用(宏嵌套/宏展開/可變參數宏)

基本原則:

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

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

 
  1.  
    #define _TOSTR(s) #s
  2.  
     
  3.  
    printf(_TOSTR(test ABC))
  4.  
    printf(_TOSTR("test ABC"));
  5.  
    printf(_TOSTR(_TOSTR(test ABC)));
  6.  
     
  7.  
    ==================
  8.  
    預編譯結果:
  9.  
    printf("test ABC")
  10.  
    printf("\"test ABC\"");
  11.  
    printf("_TOSTR(test ABC)");

##是原樣代入

 

 
  1.  
    #define HI_BOY HiBoy!
  2.  
    #define __ORIGINAL(s) s
  3.  
    #define _TOSTR(s) #s
  4.  
    #define STR_CONCAT(x, y) x##y
  5.  
     
  6.  
    printf(HI_BOY);
  7.  
    printf(__ORIGINAL(HI_BOY));
  8.  
    printf(_TOSTR(HI_BOY));
  9.  
    printf(_TOSTR(__ORIGINAL(HI_BOY)));
  10.  
    printf(STR_CONCAT(HI_BOY, HI_BOY ));
  11.  
    printf(STR_CONCAT(_TOSTR(HI_BOY), _TOSTR(HI_BOY) ));
  12.  
    ===========================================================
  13.  
    預處理後的結果:
  14.  
    printf(HiBoy!);
  15.  
    printf(HiBoy!);
  16.  
    printf("HI_BOY");
  17.  
    printf("__ORIGINAL(HI_BOY)");
  18.  
    printf(HI_BOYHI_BOY);
  19.  
    printf("HI_BOY" "HI_BOY");

連續的兩個雙引號會被忽略掉

以下寫法結果一樣:

 
  1.  
    printf("this is a test sentence. are you ok?\n");
  2.  
    printf("this is a test ""sentence."" are you ok?\n");
  3.  
    printf("this is a test sentence." " are you ok?\n");
  4.  
    printf("this is a" " test ""sentence. are"" you ok?\n");
  5.  
     
  6.  
    ==============
  7.  
    this is a test sentence. are you ok?
  8.  
    this is a test sentence. are you ok?
  9.  
    this is a test sentence. are you ok?
  10.  
    this is a test sentence. are you ok?

嵌套

 
  1.  
    #define QUOTATION "
  2.  
    #define HI_BOY HiBoy!
  3.  
    #define __ORIGINAL(s) s
  4.  
    #define _TOSTR(s) #s
  5.  
    #define STR_CONCAT(x, y) x##y
  6.  
     
  7.  
    printf(HI_BOY);
  8.  
    printf(__ORIGINAL(HI_BOY));
  9.  
    printf(_TOSTR(HI_BOY));
  10.  
    printf(_TOSTR(__ORIGINAL(HI_BOY)));
  11.  
    printf(STR_CONCAT(HI_BOY, HI_BOY ));
  12.  
    printf(STR_CONCAT( _TOSTR(HI_BOY), _TOSTR(HI_BOY) ));
  13.  
    printf( STR_CONCAT( __ORIGINAL(HI_BOY), __ORIGINAL(HI_BOY) ));
  14.  
    printf( _TOSTR(STR_CONCAT( __ORIGINAL(HI_BOY), __ORIGINAL(HI_BOY) )));
  15.  
    printf(_TOSTR(__ORIGINAL(STR_CONCAT( __ORIGINAL(HI_BOY), __ORIGINAL(HI_BOY) ))));
  16.  
    printf( ""(STR_CONCAT( __ORIGINAL(HI_BOY), __ORIGINAL(HI_BOY) ))"");
  17.  
    printf(_TOSTR(__ORIGINAL(STR_CONCAT( __ORIGINAL(HI_BOY), __ORIGINAL(HI_BOY) ))));
  18.  
    printf( QUOTATION(STR_CONCAT( __ORIGINAL(HI_BOY), __ORIGINAL(HI_BOY) ))QUOTATION);
  19.  
    printf( QUOTATION STR_CONCAT( __ORIGINAL(HI_BOY), __ORIGINAL(HI_BOY) )QUOTATION);
  20.  
     
  21.  
     
  22.  
    ==================================================
  23.  
    預編譯結果:
  24.  
    printf(HiBoy!);
  25.  
    printf(HiBoy!);
  26.  
    printf("HI_BOY");
  27.  
    printf("__ORIGINAL(HI_BOY)");
  28.  
    printf(HI_BOYHI_BOY);
  29.  
    printf("HI_BOY" "HI_BOY");
  30.  
    printf( HiBoy!HiBoy!);
  31.  
    printf( "STR_CONCAT( __ORIGINAL(HI_BOY), __ORIGINAL(HI_BOY) )");
  32.  
    printf("__ORIGINAL(STR_CONCAT( __ORIGINAL(HI_BOY), __ORIGINAL(HI_BOY) ))");
  33.  
    printf( ""(HiBoy!HiBoy!)"");
  34.  
    printf("__ORIGINAL(STR_CONCAT( __ORIGINAL(HI_BOY), __ORIGINAL(HI_BOY) ))");
  35.  
    printf( "(HiBoy!HiBoy!)");
  36.  
    printf( " HiBoy!HiBoy! ");

參考

 https://blog.csdn.net/Pillary/article/details/53705158

 

轉載於:https://my.oschina.net/SamXIAO/blog/2877664

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章