yohhoyの日記

技術的メモをしていきたい日記

fflush(NULL);

C標準ライブラリ関数fflushにヌルポインタを指定すると、プログラムが現在開いている全てのストリームをフラッシュする。

fflush(NULL);  // OK

C99 7.19.5.2/p1-3より引用(下線部は強調)。

#include <stdio.h>
int fflush(FILE *stream);

2 If stream points to an output stream or an update stream in which the most recent operation was not input, the fflush function causes anyunwritten data for that stream to be delivered to the host environment to be written to the file; otherwise, the behavior is undefined.
3 If stream is a null pointer, the fflush function performs this flushing action on all streams for which the behavior is defined above.

(PDF) C99 Rationale, 7.19.5.2より引用。

The fflush function ensures that output has been forced out of internal I/O buffers for a specified stream. Occasionally, however, it is necessary to ensure that all output is forced out, and the programmer may not conveniently be able to specify all the currently open streams, perhaps because some streams are manipulated within library packages. To provide an implementation-independent method of flushing all output buffers, the Standard specifies that this is the result of calling fflush with a NULL argument.

関連URL