![](/img/trans.png)
[英]Issue with multiplication by a fraction in pre-processor Substitution (#define)
[英]#define Pre-processor Replacement in Printf() String
我正在打印一個字符串,例如:
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預處理器不了解String內部的內容,因此不操縱String。
以下語句將THIRTY
替換為30
printf("Print then number thirty: %d", THIRTY);
預處理器可以做到這一點,但你必須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;
}
看看這個
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
作為輸出。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.