兩級巨集 字串化巨集

2022-05-01 03:36:09 字數 437 閱讀 6967

如果你想字串化巨集引數擴充套件的結果,你必須使用兩個級別的巨集。

1

#define xstr(s) str(s)

2#define str(s) #s

3#define foo 4

4str (foo)

5 ==> "

foo"

6xstr (foo)

7 ==> xstr (4

)8 ==> str (4

)9 ==> "

4"

當s被用在str中,s被字串化,所以首先它不是巨集擴充套件。

但s是xstr的乙個普通引數,所以在xstr完全巨集擴充套件之前s本身已經展開。

str到達它的引數的時候,它已經是巨集擴充套件。就是xstr處理自身引數foo的時候,foo已經展開。

解決方案出處

ATL字串轉換巨集

在標頭檔案中定義了atl提供的所有轉換巨集,如 a2cw lpcstr lpcwstr a2w lpcstr lpwstr w2ca lpcwstr lpcstr w2a lpcwstr lpstr 所有的巨集如下表所示 a2bstr ole2a t2aw2a a2cole ole2bstr t2b...

C語言巨集引數的字串化和巨集引數的連線

在巨集定義中,有時還會用到 和 兩個符號,它們能夠對巨集引數進行操作。用來將巨集引數轉換為字串,也就是在巨集引數的開頭和末尾新增引號。例如有如下巨集定義 define str s s那麼 printf s str c.biancheng.net printf s str c.biancheng.ne...

CPP 巨集的字串處理

1 define a line line define b line a line define c int b line c int 行號 2 巨集定義涉及到字串連線 字串化 則引數 line 不繼續展開 define b line line define c int b line c int l...