簡體   English   中英

#定義Printf()字符串中的預處理器替換

[英]#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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM