0
我試圖做一個簡單的測驗在Linux中,像這樣:睡眠()執行全部一次,而當它被稱爲
#include <stdio.h>
#include <unistd.h>
void main()
{
printf("Simple arithmetic\n");
printf("5 * 7 + 4/2 = ?\n");
sleep(3); //wait for 3 seconds
printf("And the answer is");
for(int i = 5; i > 0; i--){ //wait for another 5 seconds printing a '.' each second
sleep(1);
printf(" .");
}
printf("\n%d!\n", 5*7+4/2); //reveal the answer
}
問題是,這個輸出弗里斯特2級的printf的,然後等待8或秒左右,然後打印一切出這樣的:
>> Simple arithmetic
>> 5 * 7 + 4/2 = ?
>> // waits for 8 seconds instead of 3
>> And the answer is.....
>> 37! // prints everything out with no delay in-between
爲什麼會出現這種情況,我能做些什麼來解決這個問題?謝謝您的幫助!
看一看http://stackoverflow.com/questions/1716296/why-does-printf-not-flush-after-the-call-unless-a-newline-is-in-the- format-strin – nnn
'wait' undeclared。 '等待;' - >'i> 0;' – BLUEPIXY