3

I was just getting familiar with sleep(), i found that

#include<stdio.h>
int main()
{
int i=0;
printf("*********Testing Sleep***********\n");
for(i=0;i<10;i++)
{
    printf("%d",i);
    sleep(1);
}
return 0;

}

this does not print number per iteration rather dumps all numbers when gets out of loop.... but when i modify printf...

#include<stdio.h>
int main()
{
int i=0;
printf("*********Testing Sleep***********\n");
for(i=0;i<10;i++)
{
    printf("%d\n",i);
    sleep(1);
}
return 0;

}

and now as i've added '\n' new line it works as expected... why it is behaving strangely in former one...

Sebastian Mach
  • 38,570
  • 8
  • 95
  • 130
kHAzaDOOm
  • 335
  • 1
  • 2
  • 4

6 Answers6

7

This is because the output buffer isn't being flushed (in other words, actually committed to the terminal). When you write a newline, the output buffer is more likely to be (but still not always, in some cases) flushed. Many terminal implementations do this to improve performance. To force the behaviour you want, you need to call fflush(stdout); after each printf call, like this:

#include<stdio.h>
int main()
{
int i=0;
printf("*********Testing Sleep***********\n");
for(i=0;i<10;i++)
{
    printf("%d",i);
    fflush(stdout);
    sleep(1);
}
return 0;
}
Delan Azabani
  • 79,602
  • 28
  • 170
  • 210
  • 1
    Instead of calling `fflush()` every time, he could also call `setvbuf( stdout, NULL, _IONBF, 0 )` at the beginning of his program to make `stdout` unbuffered. – DevSolar Jul 29 '11 at 08:41
3

What you are looking at is line buffered output. Actually writing to output is an expensive operation, so I/O streams are usually buffered. Actually writing the buffer is deferred until a specific event is encountered. In standard C, you have three types of buffering:

  • fully buffered - the buffer is written when full.
  • line buffered - the buffer is written when a newline is encountered (your case).
  • unbuffered - the buffer is written whenever an I/O function is executed. (Good for error logging.)

Writing the buffer is called flushing. That's why there is a stdio function called fflush(). You might also want to check out setvbuf() and its parameter constants, _IOFBF, _IOLBF and _IONBF. I am sure you can figure out what they mean without looking them up. ;-)

Edit: This program delivers as you originally expected:

#include <stdio.h>

// This is the header where sleep() is declared. Don't go without it.
#include <unistd.h>

int main()
{
    int i=0;

    // setvbuf() can be called on a stream only BEFORE
    // you do any I/O on it!
    setvbuf( stdout, NULL, _IONBF, 0 );

    printf( "*********Testing Sleep***********\n" );
    for ( i = 0; i < 10; ++i )
    {
        printf( "%d", i );
        sleep( 1 );
    }
    return 0;
}
DevSolar
  • 67,862
  • 21
  • 134
  • 209
1

standard output for terminals is line buffered, output is not written unless there is a newline or you manually flush it.

Karoly Horvath
  • 94,607
  • 11
  • 117
  • 176
  • Actually there is no guarantee that your output is line buffered. It might just as well be fully buffered (i.e. when the environment can determine that your output is non-interactive, it is allowed to run I/O at fully buffered). But yes, line buffered is the most common. – DevSolar Jul 29 '11 at 08:36
  • yes, if you write to a file or a pipe and not to a terminal it's going to be fully buffered (not written until buffer is full). – Karoly Horvath Jul 29 '11 at 08:39
  • *If* the environment can assert it actually *is* a file or pipe. Implementations are at liberty to use `_IOLBF` if in doubt. – DevSolar Jul 29 '11 at 10:42
0

This is because printf() uses buffered output for better performance. Buffer is flushed to the console once \n is printed.

Sebastian Mach
  • 38,570
  • 8
  • 95
  • 130
sharptooth
  • 167,383
  • 100
  • 513
  • 979
0

Output is buffered, so that that the OS has an opportunity to optimize output speed. To make sure they are flushed immediately, do fflush (stdout);, but usually, you don't.

Sebastian Mach
  • 38,570
  • 8
  • 95
  • 130
0

Printf is buffered.

You can force printf to 'flush' its buffer using the fflush call.

Or

Simply push the buffer to stdout using \n as in your case .

More detailed discussion is here

Community
  • 1
  • 1
Sandeep Pathak
  • 10,567
  • 8
  • 45
  • 57