Use of fflush(stdin) in c

     

In this article, we’ll take a look at how we can use the fflush() function in C.

Bạn đang xem: Use of fflush(stdin) in c

The purpose of this function may not be immediately clear khổng lồ you, but you might take a hint from the name of the function.


The fflush() function in C

The fflush() function (Flush lớn File) is a function which flushes the contents of any output stream (or update stream) khổng lồ the corresponding file.


Why do we need this function, & not just something like fprintf() or printf()?

The answer is due lớn how the Operating System handles this input. Whenever we pass any output, the Operating System actually puts the nội dung (which will be eventually written to the đầu ra file – like stdout) in a buffer.

This buffer will automatically get flushed at a tệp tin only at certain instances.


Understanding the need for fflush()

To illustrate a common issue that we face due to output đầu ra buffering, consider the following code snippet.


#include int main() fprintf(stdout, "This is khổng lồ stdout. "); fprintf(stderr, "This is to stderr. "); fprintf(stdout, "This is also to stdout. ");
Why does this happen? This is because of how the files for stdout and stderr are buffered.

Xem thêm: Lý Thuyết Ôn Tập Giữa Học Kì I: Tiết 1 Tiếng Gió Rừng Vi Vu Như

By default, stdout is newline-buffered, while stderr is unbuffered.

What does this mean? For stdout, until there is a newline, the contents of the đầu ra stream are actually stored inside a temporary buffer.


Only when the output stream encounters a newline (or kết thúc of file), will the contents be written lớn the stdout file. Notice the lack of a newline in our program!

But, for stderr, the stderr stream is not buffered, so the contents are immediately written to lớn it!

So, in our example program, since we don’t have a newline, stderr will be written first. Only after the end of file (output) is reached, will the contents of stdout be written!

So if you want khổng lồ have stdout output first, without using a newline, you must flush the đầu ra buffer using fflush(). Makes sense?

Let’s now địa chỉ fflush() after writing lớn stdout, so that the contents are instantly written lớn the output đầu ra file.


#include int main() fprintf(stdout, "This is lớn stdout. "); // Flush the contents of the đầu ra stream immediately fflush(stdout); fprintf(stderr, "This is to stderr. "); // No need khổng lồ flush stderr since it is un-buffered fprintf(stdout, "This is also khổng lồ stdout. "); // Flush the contents of the output stream immediately fflush(stdout);
Output


This is khổng lồ stdout. This is to stderr. This is also khổng lồ stdout.
Indeed, we have our problem solved for us, using fflush()!

Can I use fflush(stdin) for input đầu vào Streams?

The short answer is: No, don’t ever use this function.

The lô ghích of flushing buffered output đầu ra stream khổng lồ files makes sense, since we know that the output đầu ra contents will be eventually be written to a file / stream

But for input streams, there is no way of knowing where the input will end up eventually. For all you know, there may be some external program which feeds đầu vào randomly lớn this program, và you have no way of knowing what will happen to lớn it!


So, code of the form: fflush(stdin) is undefined behavior!!!

Conclusion

In this article, we learned how we could use the fflush() function in C, to lớn flush output buffers khổng lồ files.

For similar articles, bởi vì go through our tutorial section on C programming!

References


Share on FacebookShare on TwitterShare on WhatsAppShare on RedditShare on LinkedInShare on Email
sofaxuong.vn is one of the most popular websites for Java, Python, Android, and related technical articles. Our tutorials are regularly updated, error-free, and complete. Every month millions of developers lượt thích you visit sofaxuong.vn to read our tutorials.

Xem thêm: Soạn Quá Trình Tạo Lập Văn Bản Ngắn Nhất ), Soạn Bài: Quá Trình Tạo Lập Văn Bản (Ngắn Nhất)

sofaxuong.vn was founded by Pankaj Kumar in 2010 to nói qua his experience & learnings with the whole world. He loves mở cửa source technologies and writing on sofaxuong.vn has become his passion.


Most Popular


Favorite Sites



close
Generic selectors
Exact matches only
Exact matches only
Search in title
Search in title
Search in content
Search in content
Search in excerpt
Post Type Selectors
Hidden