About printf () and fflush ()

Source: Internet
Author: User

When printf () is used for output, it is first output to the buffer and then sent from the buffer to the screen. Under what circumstances will the buffer content be sent to the screen? 1. Use fflush (stdout) to force refresh. 2. the buffer zone is full. 3. scanf () refreshes the buffer first when it needs to retrieve data in the buffer zone. 4. When \ n and \ r enter the buffer zone. 5. When the thread ends, if the thread also contains printf (...); 6, when the program ends.

# Include <stdio. h>
# Include <stdlib. h>
Int main ()
{
Int I = 89;
Int X;
Printf ("-------------- \ n"); // output ----------- without outputting 89 immediately;
Printf ("% d", I); // after this statement, add fflush (stdout); then the output will be 89 immediately
Sleep (5 );
Scanf ("% d", & X); // output the 89 to the screen after 5 seconds.
Return 0;
}

In addition, when the fclose function closes a file, the function writes the file content that has not been written back to the disk in the memory to the disk. If the fclose function is not called, wait until the buffer in the memory is filled up and the system writes the content back to the disk.

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.