Windows下的宏

來源:互聯網
上載者:User
1,防止一個標頭檔被重複包含 #ifndef COMDEF_H #define COMDEF_H   //標頭檔內容 #endif 2,重新定義一些類型,防止由於各種平台和編譯器的不同,而產生的類型位元組數差異,方便移植。 typedef  unsigned char      boolean;     /* Boolean value type. */ typedef  unsigned long int  uint32;      /* Unsigned 32 bit value */ typedef  unsigned short     uint16;      /* Unsigned 16 bit value */ typedef  unsigned char      uint8;       /* Unsigned 8  bit value */ typedef  signed long int    int32;       /* Signed 32 bit value */ typedef  signed short       int16;       /* Signed 16 bit value */ typedef  signed char        int8;        /* Signed 8  bit value */ //下面的不建議使用 typedef  unsigned char     byte;         /* Unsigned 8  bit value type. */ typedef  unsigned short    word;         /* Unsinged 16 bit value type. */ typedef  unsigned long     dword;        /* Unsigned 32 bit value type. */ typedef  unsigned char     uint1;        /* Unsigned 8  bit value type. */ typedef  unsigned short    uint2;        /* Unsigned 16 bit value type. */ typedef  unsigned long     uint4;        /* Unsigned 32 bit value type. */ typedef  signed char       int1;         /* Signed 8  bit value type. */ typedef  signed short      int2;         /* Signed 16 bit value type. */ typedef  long int          int4;         /* Signed 32 bit value type. */ typedef  signed long       sint31;       /* Signed 32 bit value */ typedef  signed short      sint15;       /* Signed 16 bit value */ typedef  signed char       sint7;        /* Signed 8  bit value */ 3,得到指定地址上的一個位元組或字 #define  MEM_B( x )  ( *( (byte *) (x) ) ) #define  MEM_W( x )  ( *( (word *) (x) ) ) 4,求最大值和最小值    #define  MAX( x, y ) ( ((x) > (y)) ? (x) : (y) )    #define  MIN( x, y ) ( ((x) < (y)) ? (x) : (y) ) 5,得到一個field在結構體(struct)中的位移量 #define FPOS( type, field ) \ /*lint -e545 */ ( (dword) &(( type *) 0)-> field ) /*lint +e545 */ 6,得到一個結構體中field所佔用的位元組數 #define FSIZ( type, field ) sizeof( ((type *) 0)->field ) 7,按照LSB格式把兩個位元組轉化為一個Word #define  FLIPW( ray ) ( (((word) (ray)[0]) * 256) + (ray)[1] ) 8,按照LSB格式把一個Word轉化為兩個位元組 #define  FLOPW( ray, val ) \   (ray)[0] = ((val) / 256); \   (ray)[1] = ((val) & 0xFF) 9,得到一個變數的地址(word寬度) #define  B_PTR( var )  ( (byte *) (void *) &(var) ) #define  W_PTR( var )  ( (word *) (void *) &(var) ) 10,得到一個字的高位和低位位元組 #define  WORD_LO(xxx)  ((byte) ((word)(xxx) & 255)) #define  WORD_HI(xxx)  ((byte) ((word)(xxx) >> 8)) 11,返回一個比X大的最接近的8的倍數 #define RND8( x )       ((((x) + 7) / 8 ) * 8 ) 12,將一個字母轉換為大寫 #define  UPCASE( c ) ( ((c) >= ''a'' && (c) <= ''z'') ? ((c) - 0x20) : (c) ) 13,判斷字元是不是10進值的數字 #define  DECCHK( c ) ((c) >= ''0'' && (c) <= ''9'') 14,判斷字元是不是16進值的數字 #define  HEXCHK( c ) ( ((c) >= ''0'' && (c) <= ''9'') ||\                        ((c) >= ''A'' && (c) <= ''F'') ||\ ((c) >= ''a'' && (c) <= ''f'') ) 15,防止溢出的一個方法 #define  INC_SAT( val )  (val = ((val)+1 > (val)) ? (val)+1 : (val)) 16,返回數組元素的個數 #define  ARR_SIZE( a )  ( sizeof( (a) ) / sizeof( (a[0]) ) ) 17,返回一個無符號數n尾的值MOD_BY_POWER_OF_TWO(X,n)=X%(2^n) #define MOD_BY_POWER_OF_TWO( val, mod_by ) \            ( (dword)(val) & (dword)((mod_by)-1) ) 18,對於IO空間映射在儲存空間的結構,輸入輸出處理   #define inp(port)         (*((volatile byte *) (port)))   #define inpw(port)        (*((volatile word *) (port)))   #define inpdw(port)       (*((volatile dword *)(port)))   #define outp(port, val)   (*((volatile byte *) (port)) = ((byte) (val)))   #define outpw(port, val)  (*((volatile word *) (port)) = ((word) (val)))   #define outpdw(port, val) (*((volatile dword *) (port)) = ((dword) (val)))  19,使用一些宏跟蹤調試 A N S I標準說明了五個預定義的宏名。它們是: _ L I N E _ _ F I L E _ _ D A T E _ _ T I M E _ _ S T D C _ 如果編譯不是標準的,則可能僅支援以上宏名中的幾個,或根本不支援。記住編譯器 也許還提供其它預定義的宏名。 _ L I N E _及_ F I L E _巨集指令在有關# l i n e的部分中已討論,這裡討論其餘的宏名。 _ D AT E _巨集指令含有形式為月/日/年的串,表示源檔案被翻譯到代碼時的日期。 原始碼翻譯到目標代碼的時間作為串包含在_ T I M E _中。串形式為時:分:秒。 如果實現是標準的,則宏_ S T D C _含有十進位常量1。如果它含有任何其它數,則實現是 非標準的。 可以定義宏,例如: 當定義了_DEBUG,輸出資料資訊和所在檔案所在行 #ifdef _DEBUG #define DEBUGMSG(msg,date) printf(msg);printf(“%d%d%d”,date,_LINE_,_FILE_) #else       #define DEBUGMSG(msg,date)  #endif 20,宏定義防止使用是錯誤 用小括弧包含。 例如:#define ADD(a,b) (a+b) 用do{}while(0)語句包含多語句防止錯誤 例如:#difne DO(a,b) a+b;\                    a++; 應用時:if(….)           DO(a,b); //產生錯誤         else 解決方案: #difne DO(a,b) do{a+b;\                    a++;}while(0) 宏中"#"和"##"的用法 一、一般用法 我們使用#把宏參數變為一個字串,用##把兩個宏參數貼合在一起. 用法: #include<cstdio> #include<climits> 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 #include<climits> 這行會被展開為: 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型的最大值,為一個變數 #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.