??
Many beginners in C + + think it's just a substitute for a newline, endl
‘\n‘
which is a one-sided view, because it endl
's not just inserted in the output stream ‘\n‘
, but it also has the effect of flushing the buffer. Input and output stream will be captured in the data stored in the buffer, when the computer is not busy, the data will be processed in real-time, ‘\n‘
and endl
there is no much difference, when the computer task is heavy, will default to wait until the buffer is filled, and then a piece of processing out of the data, when endl
starts to work, it tells the computer to start processing the data in the buffer immediately.
cout << a ;cout << b ;return 0;
For example, the above code, when the computer is very busy, it will not finish the first line to print out a value, nor in the second line of code to print out the value of B, the computer will wait until the last line of code, the end of the program, or need to output too many things, the buffer fills, will be together a
and b
the values are printed at the same time.
cout << a << endl;cout << b << endl;return 0;
Plus endl
, no matter how busy the computer is, it will immediately print out the buffer, empty the buffer, execute to the first line, the a
print out, execute to the second line, the b
print out.
The use of endl
immediately emptying buffers is a good programming habit, and any time the program is precisely manipulated is more stable and secure than the automatic processing of the computer, and in some demanding operating efficiency, it ‘\n‘
is reasonable to use it, depending on the situation.
The difference between C + + manipulator Endl and line feed ' \ n '