C宏定義探析

來源:互聯網
上載者:User

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");

 

  

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.