| |

VerySource

 Forgot password?
 Register
Search
View: 521|Reply: 3

About fflush (stdout)

[Copy link]

1

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 Invalid IP Address

Post time: 2020-1-17 18:20:01
| Show all posts |Read mode
Hello everyone,
When debugging under VC, single-step execution of each printf statement, the console can display accordingly.
Why don't you need to execute fflush (stdout)?

Normally, it should look like this
printf (.....);
fflush (stdout);
Each printf statement must be followed by an fflush statement in order to have a corresponding display. Otherwise, the information of the printf statement will be cached.

I don't know if VC has done special processing for printf when debugging, so that it does not need to be cached and output directly to the console?
Hope to get everyone's help.
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-9-2 01:15:01
| Show all posts
When I wrote the c program, I always only used printf (...), which can also display the results. Why do I have to use fflush (stdout)?

I have the same confusion about the question from the original poster. Please explain it carefully, thanks in advance!
Reply

Use magic Report

0

Threads

4

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

Post time: 2020-9-2 01:30:02
| Show all posts
If you don’t fflush, the result will be output when the system is free. This is not obvious on the MS system, but it is more obvious under linux/unix, because under linux/unix, stdout is used to reload. Redirection, such as file or printer, etc. Assuming that the redirection is a file, printf is output to the file. If you do not execute the fflush statement, the actual content of the file may be changed at some later time.
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-9-2 16:00:01
| Show all posts
The effect of using fflush(stdout) after printf() is to output the content to be output immediately.
When the printf() function is used, the system stores the content in the output buffer, and then outputs it when the time slice turns to the output program of the system.
After using fflush(out), immediately clear the output buffer and output the contents of the buffer.
E.g:
for (ctr = 1; ctr <= wait; ctr++)
{
printf("."); /* print a dot */
fflush(stdout); /* force dot to print on buffered machines */
sleep((int) 1); /* pause 1 second */
}

Use fflush(stdout) to make the machine output one. Pause for one second, without disorder (for example: PP...PP, PPP... etc.).
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list