<delect id="sj01t"></delect>
  1. <em id="sj01t"><label id="sj01t"></label></em>
  2. <div id="sj01t"></div>
    1. <em id="sj01t"></em>

            <div id="sj01t"></div>

            宏中"#"和"##"的用法

            時間:2024-06-18 18:05:20 Linux認證 我要投稿
            • 相關推薦

            關于宏中"#"和"##"的用法

              計算機科學里的宏(Macro),是一種批量批處理的稱謂。下面小編準備了關于宏中"#"和"##"的用法,提供給大家參考!

              一、一般用法

              我們使用#把宏參數變為一個字符串,用##把兩個宏參數貼合在一起.

              用法:

              #i nclude

              #i nclude

              using namespace std;

              #define STR(s) #s

              #define CONS(a,b) int(a##e##b)

              int main()

              {

              printf(STR(vck)); // 輸出字符串"vck"

              printf("%d\n", CONS(2,3)); // 2e3 輸出:2000

              return 0;

              }

              二、當宏參數是另一個宏的時候

              需要注意的是凡宏定義里有用'#'或'##'的地方宏參數是不會再展開.

              1, 非'#'和'##'的情況

              #define TOW (2)

              #define MUL(a,b) (a*b)

              printf("%d*%d=%d\n", TOW, TOW, MUL(TOW,TOW));

              這行的宏會被展開為:

              printf("%d*%d=%d\n", (2), (2), ((2)*(2)));

              MUL里的參數TOW會被展開為(2).

              2, 當有'#'或'##'的時候

              #define A (2)

              #define STR(s) #s

              #define CONS(a,b) int(a##e##b)

              printf("int max: %s\n", STR(INT_MAX)); // INT_MAX #i nclude

              這行會被展開為:

              printf("int max: %s\n", "INT_MAX");

              printf("%s\n", CONS(A, A)); // compile error

              這一行則是:

              printf("%s\n", int(AeA));

              INT_MAX和A都不會再被展開, 然而解決這個問題的方法很簡單. 加多一層中間轉換宏.

              加這層宏的用意是把所有宏的參數在這層里全部展開, 那么在轉換宏里的那一個宏(_STR)就能得到正確的宏參數.

              #define A (2)

              #define _STR(s) #s

              #define STR(s) _STR(s) // 轉換宏

              #define _CONS(a,b) int(a##e##b)

              #define CONS(a,b) _CONS(a,b) // 轉換宏

              printf("int max: %s\n", STR(INT_MAX)); // INT_MAX,int型的最大值,為一個變量 #i nclude

              輸出為: int max: 0x7fffffff

              STR(INT_MAX) --> _STR(0x7fffffff) 然后再轉換成字符串;

              printf("%d\n", CONS(A, A));

              輸出為:200

              CONS(A, A) --> _CONS((2), (2)) --> int((2)e(2))

              三、'#'和'##'的一些應用特例

              1、合并匿名變量名

              #define ___ANONYMOUS1(type, var, line) type var##line

              #define __ANONYMOUS0(type, line) ___ANONYMOUS1(type, _anonymous, line)

              #define ANONYMOUS(type) __ANONYMOUS0(type, __LINE__)

              例:ANONYMOUS(static int); 即: static int _anonymous70; 70表示該行行號;

              第一層:ANONYMOUS(static int); --> __ANONYMOUS0(static int, __LINE__);

              第二層: --> ___ANONYMOUS1(static int, _anonymous, 70);

              第三層: --> static int _anonymous70;

              即每次只能解開當前層的宏,所以__LINE__在第二層才能被解開;

              2、填充結構

              #define FILL(a) {a, #a}

              enum IDD{OPEN, CLOSE};

              typedef struct MSG{

              IDD id;

              const char * msg;

              }MSG;

              MSG _msg[] = {FILL(OPEN), FILL(CLOSE)};

              相當于:

              MSG _msg[] = {{OPEN, "OPEN"},

              {CLOSE, "CLOSE"}};

              3、記錄文件名

              #define _GET_FILE_NAME(f) #f

              #define GET_FILE_NAME(f) _GET_FILE_NAME(f)

              static char FILE_NAME[] = GET_FILE_NAME(__FILE__);

              4、得到一個數值類型所對應的字符串緩沖大小

              #define _TYPE_BUF_SIZE(type) sizeof #type

              #define TYPE_BUF_SIZE(type) _TYPE_BUF_SIZE(type)

              char buf[TYPE_BUF_SIZE(INT_MAX)];

              --> char buf[_TYPE_BUF_SIZE(0x7fffffff)];

              --> char buf[sizeof "0x7fffffff"];

              這里相當于:

              char buf[11];

            【宏中"#"和"##"的用法】相關文章:

            粵語中“定”和“先生”的用法03-08

            PHP中MySQL、MySQLi和PDO的用法和區別03-10

            php中dirname()和--FILE--常量的用法04-01

            agree with的用法和辨析11-20

            jQuery中prev()方法用法03-30

            jQuery中replaceAll()方法用法03-30

            Javascript中typeof 用法歸納04-01

            粵語中“好”字的用法03-18

            俄語詞выйти和сойти的用法與區別03-06

            <delect id="sj01t"></delect>
            1. <em id="sj01t"><label id="sj01t"></label></em>
            2. <div id="sj01t"></div>
              1. <em id="sj01t"></em>

                      <div id="sj01t"></div>
                      黄色视频在线观看