Results 1 to 4 of 4
Suppose I have code like this
int main(){
printf("hello");
sleep(1);
printf("hallo\n");
}
The result is the "hello" won't come out after one second. So it means it doesn't print until ...
- 10-28-2008 #1
\n in Linux behaviour
Suppose I have code like this
int main(){
printf("hello");
sleep(1);
printf("hallo\n");
}
The result is the "hello" won't come out after one second. So it means it doesn't print until it meets \n which it appear at "hallo\n". Is this purposely created like this? or a bug?
- 10-28-2008 #2
This is a feature. The intention is to avoid actually doing I/O until necessary, thus improving the speed of many programs.
If you wish to force output after a printf() statement, do this just after that statement:
If you wish all output to appear on the screen immediately, then instead of doing this:Code:fflush(stdout);
you can do this:Code:printf("hello");
It's understood that stderr data must be seen immediately.Code:fprintf(stderr,"hello");
For more information and flexibility, do this at the command prompt:
Hope this helps.Code:man setbuf
--
Bill
Old age and treachery will overcome youth and skill.
- 10-28-2008 #3
So \n beside make newline also flush the standard output like C++ using endl... right ? I understand now. Thanks wje_lf
- 10-28-2008 #4Yes, but with setbuf() you can change this in one of two ways: you can make it so that with each printf() or fprintf() the buffer will be flushed; or you can make it so that even /n won't flush the buffer, and the buffer won't be flushed until it's full.\n beside make newline also flush the standard output--
Bill
Old age and treachery will overcome youth and skill.


Reply With Quote