2 回答

TA貢獻1866條經驗 獲得超5個贊
像這樣:
#include <stdio.h>
#define QUOTE(str) #str
#define EXPAND_AND_QUOTE(str) QUOTE(str)
#define TEST thisisatest
#define TESTE EXPAND_AND_QUOTE(TEST)
int main() {
printf(TESTE);
}
原因是當將宏參數替換為宏主體時,除非它們與該宏中的#或##預處理運算符一起出現,否則它們將被擴展。因此,str(TEST在代碼中帶有值)不會在中擴展QUOTE,而是會在中擴展EXPAND_AND_QUOTE。

TA貢獻1786條經驗 獲得超13個贊
為了進一步說明,基本上使預處理器執行另一個“階段”。即:
第一種情況:
->TESTE
->QUOTE(TEST) # preprocessor encounters QUOTE
# first so it expands it *without expanding its argument*
# as the '#' symbol is used
->TEST
第二種情況:
->TESTE
->EXPAND_AND_QUOTE(TEST)
->QUOTE(thisisatest)
# after expanding EXPAND_AND_QUOTE
# in the previous line
# the preprocessor checked for more macros
# to expand, it found TEST and expanded it
# to 'thisisatest'
->thisisatest
- 2 回答
- 0 關注
- 437 瀏覽
添加回答
舉報