【整理】C宏定義

來源:互聯網
上載者:User

1,防止一個標頭檔被重複包含
  #ifndef BODYDEF_H
  #define BODYDEF_H
    //標頭檔內容
  #endif
2,得到指定地址上的一個位元組或字
  #define  MEM_B( x )  ( *( (byte *) (x) ) )
  #define  MEM_W( x )  ( *( (word *) (x) ) )
3,得到一個field在結構體(struct)中的位移量
  #define FPOS( type, field ) ( (dword) &(( type *) 0)-> field )
4,得到一個結構體中field所佔用的位元組數
  #define FSIZ( type, field ) sizeof( ((type *) 0)->field )
5,得到一個變數的地址(word寬度)
  #define  B_PTR( var )  ( (byte *) (void *) &(var) )
  #define  W_PTR( var )  ( (word *) (void *) &(var) )
6,將一個字母轉換為大寫
  #define  UPCASE( c ) ( ((c) >= ''a'' && (c) <= ''z'') ? ((c) - 0x20) : (c) )
7,判斷字元是不是10進值的數字
  #define  DECCHK( c ) ((c) >= ''0'' && (c) <= ''9'')
8,判斷字元是不是16進值的數字
  #define  HEXCHK( c ) ( ((c) >= ''0'' && (c) <= ''9'') ||((c) >= ''A'' && (c) <= ''F'') ||((c) >= ''a'' && (c) <= ''f'') )
9,防止溢出的一個方法
  #define  INC_SAT( val )  (val = ((val)+1 > (val)) ? (val)+1 : (val))
10,返回數組元素的個數
  #define  ARR_SIZE( a )  ( sizeof( (a) ) / sizeof( (a[0]) ) )
11,使用一些宏跟蹤調試
  ANSI標準說明了五個預定義的宏名:
    _LINE_ (兩個底線),對應%d
    _FILE_     對應%s
    _DATE_   對應%s
    _TIME_    對應%s
    _STDC_

 
宏中"#"和"##"的用法:
  使用#把宏參數變為一個字串,用##把兩個宏參數貼合在一起.
    #define STR(s)     #s
    #define CONS(a,b)  int(a##e##b)
    Printf(STR(vck));           // 輸出字串"vck"
    printf("%d\n", CONS(2,3));  // 2e3 輸出:2000

當宏參數是另一個宏的時候:
  注意:凡宏定義裡有用"#"或"##"的地方,宏參數不會再展開.
    #define A          (2)
    #define STR(s)     #s
    #define CONS(a,b)  int(a##e##b)
    printf("%s\n", CONS(A, A));               // error: 'AeA' was not declared in this scope
      printf("%s\b", STR(INT_MAX));     // INT_MAX
  INT_MAX和A都不會再被展開, 解決這個問題的方法很簡單:加多一層中間轉換宏.

  加這層宏的用意是把所有宏的參數在這層裡全部展開, 那麼在轉換宏裡的那一個宏(_STR)就能得到正確的宏參數
    #define STR(s)      _STR(s)            // 轉換宏
    #define CONS(a,b)   _CONS(a,b)        // 轉換宏
    printf("int max: %s\n", STR(INT_MAX));          // INT_MAX,int型的最大值,為一個變數 #include<climits>
      輸出為: 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]; 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.