The difference between C + + manipulator Endl and line feed ' \ n '

Source: Internet
Author: User

??
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 endlstarts 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 bthe 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 '

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.