糯米文學吧

位置:首頁 > 計算機 > C語言

C語言的宏定義分析

C語言1.6W

引導語:你瞭解C語言嗎,知道C語言的宏定義是什麼嗎,以下是本站小編分享給大家的C語言的'宏定義分析,歡迎閲讀!

C語言的宏定義分析

C語言中,預處理器功能:

1. #include <>or" " 的頭文件替換

2.#define 對象替換(object-like)

對象替換以第一個空格為分割,之後的為replacement token list

3.#define () 函數替換(function-like)

函數替換 ()之間不能有任何空白符。但是調用的時候可以在之間有空格。

函數替換的時候需要注意參數表的優先級和類型。如果替換塊中需要用--';'是,用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("%sn", QUOTEME(1+2));

替換後==>

printf("%sn", "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: %dn", _replace_1( Older, Small ) );

// The parameters to _replace_2 are substituted before the call

// to _replace_1, so we get NewerLarge.

printf("Check 2: %dn", _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=%sn", QUOTEME(FOO));

擴展後==>

printf("FOO=%sn", "bar");

標籤:語言