flushing stdin is undefined behavior, since fflush is meant to be called on an output stream. This is taken from the C standard [7.21.5.2]/2:

int fflush(FILE *ostream);

If stream points to an output stream or an update stream in which the most recent operation was not input, the fflush function causes any unwritten data for that stream to be delivered to the host environment to be written to the file; otherwise, the behavior is undefined.

you can find the standard C here in the link below, go to page 139

ANSI/ISO 9899-1990

Answer from AdamF on Stack Overflow
🌐
Cppreference
en.cppreference.com › w › cpp › io › c › fflush
std::fflush - cppreference.com
Otherwise returns EOF and sets the error indicator of the file stream. POSIX extends the specification of fflush by defining its effects on an input stream, as long as that stream represents a file or another seekable device: in that case the POSIX file pointer is repositioned to match the C stream pointer (which effectively undoes any read buffering) and the effects of any std::ungetc or std::ungetwc that weren't yet read back from the stream are discarded.
🌐
Microsoft Learn
learn.microsoft.com › en-us › cpp › c-runtime-library › reference › fflush
fflush | Microsoft Learn
December 2, 2022 - If the stream was opened in write mode, or it was opened in update mode and the last operation was a write, fflush writes the contents of the stream buffer to the underlying file or device, and the buffer is discarded. If the stream was opened in read mode, or if the stream has no buffer, the call to fflush has no effect, and any buffer is retained.
Discussions

How to use fflush(stdin) in C++ - Stack Overflow
I have a simple question (I think so) about fflush() in C++. Every time I write some code to input string in C++, I have to try many many ways because My program caused Error every time. So I will ... More on stackoverflow.com
🌐 stackoverflow.com
input - using fflush on C++ - Stack Overflow
This is program for getting interactive input. I usually use cin and cout so is it possible not using printf and scanf? ... [That won't compile (hasil is not declared anywhere). It's not C either (C has no namespaces, and no iostream header).] ... I usually use cin and cout so is it possible not using printf and scanf? Yes it's possible, use cin and cout (as usual) and not printf and scanf! ... yes. i'm doing some research and fflush... More on stackoverflow.com
🌐 stackoverflow.com
C++ - Why fflush(stdout) does not work with iostream? - Stack Overflow
Also saw something similar on ...nce.com/w/cpp/io/cout · The global objects std::cout and std::wcout control output to a stream buffer of implementation-defined type (derived from std::streambuf), associated with the standard C output stream stdout. That's where it got a little confusing, as I was also reading about flushes and noticed that fflush(stdout) simply won't work with cin/cout. For example, this sample code won't print anything: #include #include ... More on stackoverflow.com
🌐 stackoverflow.com
Difference between std::flush and std::fflush
fflush takes a FILE* as its argument. It's for the godawful stdio sterams. flush is an iomanipulator for iostreams. The question is whether the sync_with_stdio flag has been enabled (it is by default). If this is on, then the iostream is automatically flushed to (or at least transferred into stdio) when you do any stdio operation (including fflush). So yes, you only need to call fflush in that case. If you don't need worry about flushing, then sync_with_stdio makes sure anything you output to cout gets pushed out ahead of a subsequent stdio (printf, puts, etc...) operation to stdout. More on reddit.com
🌐 r/cpp_questions
6
2
January 13, 2023
🌐
Programiz
programiz.com › cpp-programming › library-function › cstdio › fflush
C++ fflush() - C++ Standard Library
The fflush() function in C++ flushes any buffered data to the respective device.
🌐
Cplusplus
cplusplus.com › reference › cstdio › fflush
fflush
In files open for update (i.e., open for both reading and writing), the stream shall be flushed after an output operation before performing an input operation. This can be done either by repositioning (fseek, fsetpos, rewind) or by calling explicitly fflush, like in this example:
🌐
cppreference.com
en.cppreference.com › w › c › io › fflush
fflush - cppreference.com
January 3, 2025 - Otherwise EOF is returned and the error indicator of the file stream is set. POSIX extends the specification of fflush by defining its effects on an input stream, as long as that stream represents a file or another seekable device: in that case the POSIX file pointer is repositioned to match the C stream pointer (which effectively undoes any read buffering) and the effects of any ungetc or ungetwc that weren't yet read back from the stream are discarded.
🌐
TutorialsPoint
tutorialspoint.com › c_standard_library › c_function_fflush.htm
C Library - fflush() function
The C library fflush() function flushes the output buffer of a stream.This function forces a write of all buffered data for the given output or update stream to the file. When applied to an input stream, its behavior is undefined.
🌐
Educative
educative.io › answers › how-to-use-fflush-in-cpp
How to use fflush() in C++
The fflush function is a C library function that flushes the output buffer of the stream. In the case of standard output, the output buffer is moved to the console.
Find elsewhere
🌐
7-Zip Documentation
documentation.help › C-Cpp-Reference › fflush.html
fflush - C/C++ Reference - Documentation & Help
cppreference.com > Standard C I/O > fflush · fflush · Syntax: #include <cstdio> int fflush( FILE *stream ); If the given file stream is an output stream, then fflush() causes the output buffer to be written to the file. If the given stream is of the input type, then fflush() causes the input ...
🌐
Codeforces
codeforces.com › blog › entry › 87538
fflush() C++ codeforces - Codeforces
vector <string> ans; int main() { int t, a, b; string s; cin >> t; for (int i = 0; i < t; i++) { cin >> a >> b; fflush(stdin); getline(cin, s); ans.push_back(solve(a, b, s)); } for (int i = 0; i < ans.size(); i++) cout << ans[i] << endl; }
🌐
Vultr Docs
docs.vultr.com › cpp › standard-library › cstdio › fflush
C++ cstdio fflush() - Flush Stream Buffer | Vultr Docs
September 27, 2024 - The fflush() function in C++ is a standard library function found within the <cstdio> header, utilized primarily to flush a stream's output buffer.
🌐
GitHub
github.com › MicrosoftDocs › cpp-docs › blob › main › docs › c-runtime-library › reference › fflush.md
cpp-docs/docs/c-runtime-library/reference/fflush.md at main · MicrosoftDocs/cpp-docs
If the stream was opened in write mode, or it was opened in update mode and the last operation was a write, fflush writes the contents of the stream buffer to the underlying file or device, and the buffer is discarded. If the stream was opened in read mode, or if the stream has no buffer, the call to fflush has no effect, and any buffer is retained.
Author   MicrosoftDocs
🌐
GeeksforGeeks
geeksforgeeks.org › c language › use-fflushstdin-c
Use of fflush(stdin) in C - GeeksforGeeks
September 15, 2023 - The code above takes only single input and gives the same result for the second input. Reason is because as the string is already stored in the buffer i.e. stream is not cleared yet as it was expecting string with spaces or new line. So, to handle this situation fflush(stdin) is used.
🌐
7-Zip Documentation
documentation.help › CppReference › fflush.html
fflush - C++ Reference Documentation
If the given file stream is an output stream, then fflush() causes the output buffer to be written to the file. If the given stream is of the input type, then fflush() causes the input buffer to be cleared. fflush() is useful when debugging, if a program segfaults before it has a chance to ...
🌐
Quora
quora.com › What-is-the-use-of-fflush-stdin-in-C-or-C
What is the use of fflush(stdin) in C or C++? - Quora
Answer (1 of 4): It is used to flush output buffers. Normal write operations, for example, will buffer output until a time that is convenient for the computer. As far as the program is concerned, the data is gone (for example, written to the ...
🌐
Linux Man Pages
man7.org › linux › man-pages › man3 › fflush.3.html
fflush(3) - Linux manual page
C89, POSIX.1-2001, POSIX.1-2008. POSIX.1-2001 did not specify the behavior for flushing of input streams, but the behavior is specified in POSIX.1-2008. Note that fflush() flushes only the user-space buffers provided by the C library.
🌐
Pulo
kev.pulo.com.au › pp › RESOURCES › cplusplus › ref › cstdio › fflush.html
cstdio: fflush - C++ reference
Flush a stream. If the given stream has been opened for writing operations the output buffer is phisically written to the file. If the stream was open for reading operations the content of the input buffer is cleared. The stream remains open after this call. When a file is closed all the buffers ...