C語言學習之#define用法

來源:互聯網
上載者:User

說到#define大家首先應該想到的是宏定義,對頭,但是這有什麼問題呢? 

今天在看PG代碼的時候發現追溯到深處有些宏定義相當的古怪,比如#define Conn(x,y) x##y,這是什麼意思呢?

於是Google一下,找到答案:

#define Conn(x,y) x##y 

#define ToChar(x) #@x

#define ToString(x) #x

 

x##y表示什嗎?表示x串連y,舉例說:

int  n = Conn(123,456);  結果就是n=123456;

char* str = Conn("asdf", "adf"),結果就是 str = "asdfadf"; 

再來看#@x,其實就是給x加上單引號,結果返回是一個const char。舉例說:

char a = ToChar(1);結果就是a='1';

做個越界實驗char a = ToChar(123);結果是a='3';

但是如果你的參數超過四個字元,編譯器就給給你報錯了!error C2015: too many characters in constant   :P

最後看看#x,估計你也明白了,他是給x加雙引號

char* str = ToString(123132);就成了str="123132";

最後留幾個小實驗給大家去測測:

#define Dec(x,y) (x-y)

int n = Dec( A(123,1), 1230);

n = Conn(123, Conn(123,332) );

char* str = A("12", ToString( Dec(3,1));

結果會如何呢? 嘿嘿嘿嘿~ 

 

 

 

 

相關文章

聯繫我們

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