u-boot源碼宏 MK_STR

來源:互聯網
上載者:User

在分析u-boot源碼發現這樣的宏:
/*
* Macros to transform values
* into environment strings.
*/
#define XMK_STR(x) #x
#define MK_STR(x) XMK_STR(x)

從注釋來看是把x轉換成字串。

找到了相關的資料,解讀了這個define,還順便認識了另外兩個不常用的define

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

聯繫我們

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