I/O is the slowest part of a program. It's faster to do a lot of I/O once than to do a little I/O a bunch of times.
So when you say:
Code:
cout << "Hello, ";
cout << "my name is ";
cout << name;
cout << ".\n";
cout << "Who might ye be?\n";
Instead of doing I/O for each call of the << operator, the strings get placed into cout's buffer. When the buffer gets full, cout dumps them all to wherever it's putting the characters (the screen, in many cases).
(Normally, cout automatically gets flushed when you use cin and when the program ends, so you usually see everything when you would expect to.)
That means that, in this program:
Code:
int main()
{
cout << "Hello, world!\n";
int *p = 0;
*p = 5; // dereference a null pointer
}
the string "Hello, world!\n" will probably (unless the buffer is really small) still be in cout's buffer when the program crashes, so it'll never get written to the screen.
Doing
Code:
cout << "Hello, world!\n" << flush
flushes the contents of cout's buffer onto the screen, so if something weird happens, at least the buffer got written.
This is usually more important with files than with cout, but it can be useful for printing debugging messages right before program crashes.
Note that endl automatically flushes the buffer, so these two lines are equivalent:
Code:
cout << "Hello, world!\n" << flush;
cout << "Hello, world!" << endl;