A bug in the C + + language

Source: Internet
Author: User

Output statements Whether it is the C-language printf () or cout << "" << Endl;

A bug appears in the Loop statement:

Here are two things that are not normal:

The following are normal:

Possible causes:

"\ n" indicates a string with the content as a carriage return character. The Std::endl is a stream operator, and the output is similar to the output "\ n", but may be slightly different.
Std::endl outputs a newline character and immediately flushes the buffer.
For example
Std::cout << Std::endl;
Equivalent
Std::cout << ' \ n ' << std::flush;, or
Std::cout << ' \ n '; Std::fflush (stdout);.
Because of the overloads of the stream operator operator<<, the output is the same for ' \ n ' and ' \ n '.
For streams with output buffering (for example, cout, clog), if the buffer flush operation is not performed manually, the output will be refreshed automatically when the buffer is full. However, for cout (relative to the file output stream, etc.), the buffering is generally not obvious. But it is generally a good practice to use Endl instead of ' \ n ' when necessary.
For unbuffered streams (such as standard error output stream cerr), refresh is unnecessary and can be used directly with ' \ n '.
----
Because of the direct input/output and operating system-related, it may be necessary to switch the kernel State/user state, a certain amount of time overhead, frequent operation will greatly reduce the efficiency of the input/output, so the standard library convection input/output operation using buffering. Specifically, a relatively fixed size (buffer) is stored in memory to store temporary input or output. When necessary, the contents of the buffer are copied to the system device and the buffer is emptied, a process called flushing.

A bug in the C + + language

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.