C巨集定義的簡單總結, 的用法

2021-08-25 06:23:44 字數 3466 閱讀 4408

今天在網上突然發現了下面幾個關於c**中的巨集定義的說明,回想下,好像在系統的**中也見過這些零散的定義,但沒有注意,看到別人總結了下,發現果然很有用,雖然不知有的道可用與否,但也不失為一種手段,所以就先把它摘抄下來,增加一點見識:

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)); // compile error

這一行則是:

printf("%s/n", int(aea));

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

輸出為: 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)

enum idd;

typedef struct msgmsg;

msg _msg = ;

相當於:

msg _msg = ,

}; 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];

C語言 巨集定義的用法總結

define 函式 define debug x x x 如上所示,用於對數值表示式進行求值的巨集定義都應該用這種方式加上括號,避免在使用巨集時由於引數中的操作等或鄰近操作符之間不可預料的相互作用。define 替換 1.在呼叫巨集時,首先對引數進行檢查,看看是否包含任何由 define 定義的符號...

define巨集定義的簡單總結

1 define pi 3.14159262 define char a a 3 define string a a 1 define square x x x 2 3 inta square 2 4 intb square 2 3 使用引數的巨集定義中,注意在巨集的替換主體部分對引數使用括號。上例...

C巨集定義 和 的用法

1 是用來把變數名轉換為字串的,例如 define print val printf val d n val int main 通過預編譯 gcc e test.c o test.i 得到結果 int main 其作用相當於為輸入值新增了一對雙引號。2 用於連線字元,例如 define name n...