標(biāo)題: 關(guān)于printf輸出問題 [打印本頁]
作者: liuyy 時(shí)間: 2015-1-11 23:42
標(biāo)題: 關(guān)于printf輸出問題
printf,我們平時(shí)在用的時(shí)候沒感覺這個(gè)命令有什么缺點(diǎn),但是printf在應(yīng)用的時(shí)候一定要在雙引號里面加’\n’,printf只有在遇到’\n’的時(shí)候才輸出,主要有下面幾種情況:1.有輸出,系統(tǒng)結(jié)束自動(dòng)加\n
int main(){
printf(“hello”);
return 0;
}
2. 有輸出,強(qiáng)制刷緩存
int main(){
printf(“hello”);
fflush(stdout);
while(1);
return 0;
}
3.有輸出,緩存區(qū)滿了輸出
int main(){
while(1)
printf(“hello”);
return 0;
}
4.沒有輸出,程序未結(jié)束,沒有’\n’
intmain(){
printf(“hello”);
while(1);
return 0;
}
我們一般用的時(shí)候都是把printf和scanf一起用,像這樣
printf(“輸入:”);
scanf(“%d”,&i);
或者這樣:
printf(“輸入:”);
c = getchar();
這兩種情況都是有輸出的,原因是scanf 和getchar()處理了’\n’
歡迎光臨 (http://www.torrancerestoration.com/bbs/) |
Powered by Discuz! X3.1 |