C語言中,前置處理器功能:
1. #include <>or" " 的標頭檔替換
2.#define <identifier> <replacement token list> 對象替換(object-like)
對象替換以第一個空格為分割,之後的為replacement token list
3.#define <identifier>(<parameter list>) <replacement token list> 函數替換(function-like)
函數替換 <identifier>(<parameter list>)之間不能有任何空白符。但是調用的時候可以在之間有空格。
函數替換的時候需要注意參數表的優先順序和類型。如果替換塊中需要用——';'是,用do{}while(0)封裝,
另外注意宏定義末尾不能有';'否則if-else語句的時候容易出錯。
4 #ifdefine等條件編譯選項
宏定義中比較容易搞錯的是##與#的使用。
##是串連兩個參數,
#define MYCASE(item,id) \
case id: \
item##_##id = id;\
break
switch(x) {
MYCASE(widget,23);
}
MYCASE(widget,23); 被擴充為
case 23:
widget_23 = 23;
break;
#是把參數變為字串
#define QUOTEME(x) #x
printf("%s\n", QUOTEME(1+2));
替換後==>
printf("%s\n", "1+2");
在使用##與#的時候,如果想使宏定義的參數也被宏替換(使用其值)
而不是參數名字被使用,應該使用間接訪問的方式。
下面是兩個例子:
-----------------------------------------------------------------------------------------------------------
enum {
OlderSmall = 0,
NewerLarge = 1
};
#define Older Newer
#define Small Large
#define _replace_1(Older, Small) Older##Small
#define _replace_2(Older, Small) _replace_1(Older, Small)
void printout( void )
{
// _replace_1( Older, Small ) becomes OlderSmall (not NewerLarge),
// despite the #define calls above.
printf("Check 1: %d\n", _replace_1( Older, Small ) );
// The parameters to _replace_2 are substituted before the call
// to _replace_1, so we get NewerLarge.
printf("Check 2: %d\n", _replace_2( Older, Small ) );
}
results is:
Check 1: 0
Check 2: 1
-----------------------------------------------------------------------------
#define FOO bar
#define QUOTEME_(x) #x
#define QUOTEME(x) QUOTEME_(x)
the code
printf("FOO=%s\n", QUOTEME(FOO));
擴充後==>
printf("FOO=%s\n", "bar");