C 宏定義詳解程序員
咱們可能要定義不少常量( 不論是放在源文件仍是頭文件 ),那麼咱們有時考慮定義某個常量時,咱們就必須返回檢查原來此常量是否認義,但這樣作很麻煩.if defined宏正是爲這種狀況提供瞭解決方案.舉個例子,以下:
#define ....
#define ....
........
........
#define a 100
.......
此時,咱們要檢查a是否認義(假設咱們已經記不着這點了),或者咱們要給a一個不一樣的值,就加入以下句子
#if defined a
#undef a
#define a 200
#endif
上述語句檢驗a是否被定義,若是被定義,則用#undef語句解除定義,並從新定義a爲200
一樣,檢驗a是否認義:
#ifndef a //若是a沒有被定義
#define a 100
#endif
以上所用的宏中:#undef爲解除定義,#ifndef是if not defined的縮寫,即若是沒有定義。
這就是#if defined 的惟一做用數組
經常使用宏定義總結
*pclint///////////////////////////////////*lint -save -e* */ /*此處屏蔽全部告警*/#include <stdio.h>/*lint -restore */ /*此處打開lint 告警檢查*/
/////////////////////////////////*經過開關定義打印,1開0關,且調 用處不須要宏控制////////////////////////////////*DEBUG DEFINE BEGIN*/#define VPLS_EXT_DEBUG 1 /*開關*/
#if VPLS_EXT_DEBUG#define _VE_DEBUG(msg...) printf("[VPLS_DEBUG] %s,%d => ",__FILE__, __LINE__);printf(msg);printf("/r/n")#else#define _VE_DEBUG(x1,x2) (void)(x1),(void)(x2)#endif/*DEBUG DEFINE END*/
調用直接寫爲_VE_DEBUG("XXXXX %d",adb)開關關閉後這句直接失效//////////////////////////////
main(void){ int a,b,c,d; a = b = c = d = 9;
_BUG("a = %d,b = %d,c = %d,d = %d/n",a,b,c,d);
}
/* 斷言,只提示不處理*/#define assert(e)/ ((void)((e) || fprintf(stderr,"line %d: 'assert error!'/n",__LINE__)))////////////////////////////////////__attribute__ 做用- - __attribute__ 是GCC的關鍵字,描述變量的屬性。
下面舉幾例內核中常見的:
__attribute__((regparm(0))) int printk(const char * fmt, ...) __attribute__ ((format (printf, 1, 2)));禁止printk使用寄存器傳遞調用參數,並將printk的參數1做爲printf格式串,從參數2開始檢查其類型;
void __switch_to(struct task_struct *prev, struct task_struct *next) __attribute__((regparm(3))) ;__switch_to保留3個寄存器用做傳遞參數;
void __attribute__ ((__section__ (".text.init"))) mem_init();將mem_init編繹到.text.init段;
struct tasklet_head tasklet_vec[32 ] __attribute__((__aligned__((32)),__section__(".data.cacheline_aligned"))) ;將tasklet_vec[32]編繹到.data.cacheline_aligned段,並將它在32字節邊界上對齊;
void do_exit(long error_code)__attribute__((noreturn));do_exit不會返回;
struct Xgt_desc_struct { unsigned short size; unsigned long address __attribute__((packed));};將address在結構中緊湊排列
今 天,和一位網友Oasis談了一些關於宏定義的一些問題,很有所得。特錄於此。原由是他看到open sourc中常常有這樣的源碼,感受沒法理解:#define swap(a,b) do {int tmp;tmp = (a);(a) = (b);(b) = tmp;}while(0)。總感受這樣的定義中的do-while有點蛇足的味道,感受真的是沒有什麼必要。開始我也是這樣以爲的,由於是 while(0)嘛,總感受不用do-while,只用{}也是能夠解決問題的。後來咱們通過測試得出這樣的結論:用這個語句,徹底是程序員追求嚴謹的性 格決定的風格。由於若是這樣定義宏,那麼如上面的swap宏,就只能像函數同樣使用,這樣在句子後面跟上分號:swap(a,b);。而若是隻用{}來定 義的話,那麼使用中加不加分號就均可以了,就會比較混亂並且不嚴謹。
若是使用{}的話,會在if else語句中加分號很差用。並且,強制加分號,代表了這個宏不能夠看成操做數來使用。
C 語言宏定義技巧- - C語言宏定義技巧(經常使用宏定義) 寫好C語言,漂亮的宏定義很重要,使用宏定義能夠防止出錯,提升可移植性,可讀性,方便性 等等。下面列舉一些成熟軟件中經常使用得宏定義。。。。。。
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 ) ( (© >= 'a' && © <= 'z') ? (© - 0x20) : © )
13,判斷字符是否是10進值的數字
#define DECCHK( c ) (© >= '0' && © <= '9')
14,判斷字符是否是16進值的數字
#define HEXCHK( c ) ( (© >= '0' && © <= '9') ||/
(© >= 'A' && © <= 'F') ||/
(© >= 'a' && © <= '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)
用這個宏來跟蹤錯誤測試