天天看點

從一段小小的C語言程式說起(3)

上程式:

#include <stdio.h>

#include <sys/wait.h>

int main(void)

{

while(1)

{

fprintf(stdout,"hello-std-out");

fprintf(stderr,"hello-std-err");

sleep(1);

}

return 0;

}

問題是: 程式隻輸出hello-std-err,并不見得會輸出 hello-std-out,why?

因為stdout是塊裝置,stderr則不是。對于塊裝置,隻有當下面幾種情況下才會被輸入,1)遇到回車,2)緩沖區滿,3)flush被調用。而stderr則不會。

修改一下:

#include <stdio.h>

#include <sys/wait.h>

int main(void)

{

while(1)

{

fprintf(stdout,"hello-std-out/n");

fprintf(stderr,"hello-std-err/n");

sleep(1);

}

return 0;

}

OK!

從一段小小的C語言程式說起(3)

------------------------------------------------------------------------------------------------------

轉載我部落格文章鄭重聲明:技術性網站著名原創作者即可轉載,商業性網站必須經過我的同意才能轉載,否則追究責任——

pang123hui的部落格:

部落格園http://www.cnblogs.com/pang123hui/

CSDNhttp://blog.csdn.net/pang123hui/