fclose(3)

fclose3

Close a stream

Usage

#include <stdio.h>

int fclose(FILE *stream);

Library

Standard C library (libc, -lc)

Overview

The fclose() function flushes the stream pointed to by stream (writing any buffered output data using fflush(3)) and closes the underlying file descriptor.

Return value

Upon successful completion, 0 is returned. Otherwise, EOF is returned and errno is set to indicate the error. In either case, any further access (including another call to fclose()) to the stream results in undefined behavior.

Errors

EBADF

The file descriptor underlying stream is not valid.

The fclose() function may also fail and set errno for any of the errors specified for the routines close(2), write(2), or fflush(3).

Attributes

For an explanation of the terms used in this section, see attributes(7).

InterfaceAttributeValue
fclose()Thread safetyMT-Safe

Notes

Note that fclose() flushes only the user-space buffers provided by the C library. To ensure that the data is physically stored on disk the kernel buffers must be flushed too, for example, with sync(2) or fsync(2).

Releases

19 releases since Debian 1.1 (Buzz); the text changed 16 times.

  1. 1.1–1.3
  2. 2.0
  3. 2.1
  4. 2.2
  5. 3.0
  6. 3.1
  7. 4.0
  8. 5.0
  9. 6.0
  10. 7
  11. 8
  12. 9
  13. 10
  14. 11
  15. 12
  16. 13
  17. Arch

Standards

C11, POSIX.1-2008.

History

C89, POSIX.1-2001.

From man-pages 6.19-1 in Arch Linux 2026-11-01. Parsed by carta from the page's man source.