When does cout flush?

There is no strict rule by the standard – only that endl WILL flush, but the implementation may flush at any time it “likes”.

And of course, the sum of all digits in under 400K is 6 * 400K = 2.4MB, and that’s very unlikely to fit in the buffer, and the loop is fast enough to run that you won’t notice if it takes a while between each output. Try something like this:

 for(int i = 0; i < 100; i++)
 {
   cout<<i<<"\n";
   Sleep(1000);
 }

(If you are using a Unix based OS, use sleep(1) instead – or add a loop that takes some time, etc)

Edit: It should be noted that this is not guaranteed to show any difference. I know that on my Linux machine, if you don’t have a flush in this particular type of scenario, it doesn’t output anything – however, some systems may do “flush on \n” or something similar.

Leave a Comment