#include<stdio.h>
void main()
{
printf(5+"good morning");/*need explanation for this line
return 0;
}
該程序的輸出是 - morning 任何人都可以解釋一下嗎?以下程序的輸出說明
#include<stdio.h>
void main()
{
printf(5+"good morning");/*need explanation for this line
return 0;
}
該程序的輸出是 - morning 任何人都可以解釋一下嗎?以下程序的輸出說明
程序的輸出是 - 早上
printf(5+"good morning");
打印的" "
字符串裏面,overpassing前五個字符。因此,前四個字符g
,o
,o
,d
第五個字符空格將被超過,字符串的其餘部分將被打印。
printf
的
int printf(const char *format, ...);
這裏format
原型是一種類型的const char*
並指向文字串的第一個元素的地址。當你在printf
中通過5+"Good morning"
時,你真正傳遞的是字符串的內存地址加上5
。加上5
意味着打印將在字符串的開始之後開始5
chars
,並且單詞「Good」之後的空間計爲char。
當你撥打電話5+"good morning"
參數被轉換爲指針。這意味着字符串常量"good morning"
存儲在可執行文件的某處並且編譯器傳遞它的指針。是這樣的:
const char txt[]="good morning\0";
printf(5+txt);
所以printf
將獲得評價指針txt+5
其繞過前5個字符在字符串中(作爲一個char是單字節和8位上字尋址機單個存儲器地址)。
Printf()
方法,用來打印在()
它打印僅「早晨」的文字和5+繞過初始5個字符,即「G」「○」「○」「d」和一個「」(空格)
歡迎來到C,我們在這裏混淆了。現在嘗試解釋它的作用:'printf((char * const const const const const)?? <&5 ??(「早上好」:>%>);'。 – Lundin