我打印一個字符串,如:的#define預處理器更換()字符串
printf("Print the number thirty: 30\n");
如果我做出如下定義
#define THIRTY 30
現在
printf("Print the number thirty: THIRTY");
C預處理器替換字符串中的THIRTY --> 30
?
還是我必須去:
printf("Print then number thirty: %d", THIRTY);
我打印一個字符串,如:的#define預處理器更換()字符串
printf("Print the number thirty: 30\n");
如果我做出如下定義
#define THIRTY 30
現在
printf("Print the number thirty: THIRTY");
C預處理器替換字符串中的THIRTY --> 30
?
還是我必須去:
printf("Print then number thirty: %d", THIRTY);
C預處理器不理解什麼是內部字符串,因此不處理字符串。
下面的語句將取代THIRTY
與30
printf("Print then number thirty: %d", THIRTY);
如果你stringfy使用宏預處理器可以做到這一點... – LPs
爲什麼投票呢?答案與問題問題一致。 –
printf("Print the number thirty: THIRTY"); // it will consider is whole as a string
這將只是在輸出打印Print the number thirty: THIRTY
。
你的第二個說法 -
printf("Print then number thirty: %d", THIRTY); //you probably need this
將打印 - Print then number thirty:30
作爲輸出。
預處理器可以做到這一點,但你必須stringfy的定義。
#define xstr(s) str(s)
#define str(s) #s
#define THIRTY 30
#define TEST "Print the number thirty: " xstr(THIRTY) "\n"
int main()
{
printf(TEST);
return 0;
}
看看THIS
怎麼樣,也許,很快嘗試? –