我的朋友給了我一個謎語。我運行它。但沒有獲得預期的產出。 代碼是:爲什麼打印到「stdout」不能順序執行?
#include <stdio.h>
#include <unistd.h>
int main()
{
while(1)
{
fprintf(stdout,"hello-out");
fprintf(stderr,"hello-err");
sleep(1);
}
return 0;
}
輸出不打印hello-out。 相反,它的打印像這樣無限:
hello-errhello-errhello-errhello-errhello-errhello-errhello-errhello-errhello-errhello-err
然後我試圖像這樣:
#include <stdio.h>
#include <unistd.h>
int main()
{
int i = 0;
while(i <= 5)
{
fprintf(stdout,"hello-out");
fprintf(stderr,"hello-err");
sleep(1);
i++;
}
return 0;
}
的optput是:
hello-errhello-errhello-errhello-errhello-errhello-errhello-outhello-outhello-outhello-outhello-outhello-out
在C語言指令逐行執行線。但爲什麼它不在這裏?
C程序有*兩*輸出流。它取決於你的平臺如何呈現這些。除非你努力分開記錄這兩個流,否則擔心這一點不會太令人滿意。 –