A brief introduction to the use of fflush (stdout)

Source: Internet
Author: User

#include <stdlib.h>
#include <stdio.h>

int sum;//sum is a global variable

Main ()
{
int i;//i is a local variable

sum = 0;
for (i = 1;i <= 5; i++)//iterate i from 1 to 5
{
printf ("The value of I is%d/n", i);
Fflush (stdout);//flush the buffer
sum + = i;
}
printf ("The sum is%d/n", sum);
Exit (0);//terminate the program
}

This simple C program uses fflush (stdout) to empty the buffer and force the results to appear on the screen immediately.

General Writing C program, I have only used the printf (...). ), add fflush (stdout) seems to be no different, are immediately show the results, but with fork () when the effect is different.

#include <stdio.h>
#include <unistd.h>
int main ()
{
printf ("Hello");
Fflush (stdout);
Fork ();
return 0;
}

Invoke printf (..... , the output is generally cached by the standard library and may not necessarily be written to the output device in time, but Fflush (stdout) can force the output of the cached content.

Do not know the above understanding is right, look at the master pointing ...

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.