1

hi i the following is a simple code with while 1, but when i execute it , first it should print the first line in printf and then sleep for 1 sec and then print the second line and it should keep on doing but here i don't get anything in the terminal and after few seconds suddenly everything is printed and then goes back to sleep . what is happening exactly i am not understanding .

int main(void)
{
  while(1)
 {
    printf("hello before sleep");
    sleep(1);
    printf("hello after sleep");
  }
}

but in the same code above if i use \n after every line in the printf it works fine as i expected . why is it so ?

Naggappan Ramukannan
  • 2,000
  • 7
  • 26
  • 48
  • 2
    Buffering: http://stackoverflow.com/questions/1716296/why-does-printf-not-flush-after-the-call-unless-a-newline-is-in-the-format-strin – Adriano Repetti Jul 25 '13 at 13:45
  • 2
    Add a `\n` to your strings. See http://stackoverflow.com/questions/1716296/why-does-printf-not-flush-after-the-call-unless-a-newline-is-in-the-format-strin for why. – Sneftel Jul 25 '13 at 13:46

6 Answers6

2

printf and more generally output functions are buffered. If you want to see the expected behavior you should flush the output before going to sleep.

fflush(stdout);
sleep(1);
hivert
  • 10,116
  • 3
  • 27
  • 51
2

printf will only flush when it encounters a newline, or if its output buffer is full. So, to control the exact time at which output is actually shown either you add newline to your output, or force a flush to ensure pending output is actually sent to the terminal:

int main(void)
{
  while(1)
 {
    printf("hello before sleep");
    fflush(stdout);
    sleep(1);
    printf("hello after sleep");
    fflush(stdout);      }
}
fvu
  • 31,143
  • 5
  • 57
  • 77
1

printf is an expensive call behind the scenes. If you've printed half a line, the output is buffered in the hope that more text will be appended so it can all be displayed with a single system call.

MSalters
  • 159,923
  • 8
  • 140
  • 320
0

you could avoid the buffer issue by using cout and setting the unitbuf flag. see example

in addition, the sleep() function halts execution in milliseconds. If you wanted to halt execution for a whole second the parameter passed would be 1000, as in sleep(1000);

Community
  • 1
  • 1
dan-0
  • 189
  • 1
  • 9
  • 1
    On [VS(Windows) milliseconds](http://msdn.microsoft.com/en-us/library/windows/desktop/ms686298%28v=vs.85%29.aspx) on [POSIX system seconds](http://linux.die.net/man/3/sleep)... – fvu Jul 25 '13 at 14:06
  • The question is about C though, cout is C++ – Alex Jul 25 '13 at 14:31
  • sleep() function takes number of seconds, not milliseconds. Sleep (on windows at least) takes number of milliseconds. I don't even know if it's C++/C or unix/Windows or Everything/VC++, but one is lower case, the other is upper case. – Alex Jul 25 '13 at 14:46
0

In fact. you code is right.

But, the output stream for terminal is line buffer in default.

So, if you use '\n' or call fflush(),it flush the buffer and print the words right after you code call printf.

If you don't do that, you output words "hello before sleep" and "hello after sleep" are all stored in buffer ,when program end, it call exit function, close process and flush buffer ,print words.

Lidong Guo
  • 2,627
  • 2
  • 14
  • 27
0

As everyone else pointed out you're experiencing a flushing issue, not a sleep issue. printf is buffered, maybe in your environment it's not buffered when string contains "\n", but I think that's just your environment.

I believe, the reason you think it goes to sleep "again" when you don't have "\n", is because without "\n", the final line doesn't look normal, as it doesn't have "\n", so you environment might be printing the prompt in some odd way - like right after "hello after sleep".

So do the things others suggested to clear buffering, either fflush(stdout) (after each printf in your case), or setbuf(stdout, NULL) (this is usually done before any printf), or just plain change your printf to fprintf(stderr, "hello before sleep"); (stderr does not get buffered)

Last note: you can also stop in debugger and observe the behavior. After initial 'printf', you may or may not see output of printf on screen, but when you execute 'sleep', the debugger should hang for a second.

Alex
  • 1,172
  • 13
  • 28