標(biāo)題: 關(guān)于printf輸出問(wèn)題 [打印本頁(yè)]
作者: liuyy 時(shí)間: 2015-1-11 23:42
標(biāo)題: 關(guān)于printf輸出問(wèn)題
printf,我們平時(shí)在用的時(shí)候沒(méi)感覺(jué)這個(gè)命令有什么缺點(diǎn),但是printf在應(yīng)用的時(shí)候一定要在雙引號(hào)里面加’\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.沒(méi)有輸出,程序未結(jié)束,沒(méi)有’\n’
intmain(){
printf(“hello”);
while(1);
return 0;
}
我們一般用的時(shí)候都是把printf和scanf一起用,像這樣
printf(“輸入:”);
scanf(“%d”,&i);
或者這樣:
printf(“輸入:”);
c = getchar();
這兩種情況都是有輸出的,原因是scanf 和getchar()處理了’\n’
| 歡迎光臨 (http://www.raoushi.com/bbs/) |
Powered by Discuz! X3.1 |