ferror(3)

๐Ÿ’– ferror(3) โ€” the girlies' guide to checking & resetting stream status ๐Ÿ’–

okay so bestie, this is a page from the Library Functions Manual, and it's about three little functions that all live in <stdio.h> and are lowkey the bouncers of your file streams. they check the vibes. they reset the vibes. let's get into it. โœจ


๐ŸŒธ NAME ๐ŸŒธ

clearerr, feof, ferror โ€” check and reset stream status, bestie. that's it. that's the job. ๐Ÿ’…


๐Ÿ“š LIBRARY ๐Ÿ“š

Standard C library, aka libc, link it with -lc. iconic, standard, we love her. ๐Ÿซถ


โœจ SYNOPSIS โœจ

#include <stdio.h>

void clearerr(FILE *stream);
int feof(FILE *stream);
int ferror(FILE *stream);

look at them. three little icons. ๐ŸŽ€


๐Ÿ’œ DESCRIPTION ๐Ÿ’œ

so here's the tea, one function at a time:

  • clearerr() โ€” this one clears the end-of-file indicator and the error indicator for the stream that stream points to. it's the factory reset. the clean slate. the "we don't talk about that anymore" button. ๐Ÿงน
  • feof() โ€” this one tests the end-of-file indicator for the stream that stream points to, and it returns nonzero if that indicator is set. and here's the important part: that end-of-file indicator can only be cleared by clearerr(). nobody else. not you, not your other functions, only clearerr(). periodt. ๐Ÿ‘‘
  • ferror() โ€” this one tests the error indicator for the stream that stream points to, returning nonzero if it's set. and same energy as above: the error indicator can be reset only by the clearerr() function. she's the only one with the keys. ๐Ÿ”‘

and if you're out here looking for the nonlocking counterparts of these? go check unlocked_stdio(3), bestie. that's where they live. ๐Ÿฆ‹


๐ŸŽฏ RETURN VALUE ๐ŸŽฏ

  • feof() returns nonzero if the end-of-file indicator is set for stream, otherwise it returns zero. simple as that. โœจ
  • ferror() returns nonzero if the error indicator is set for stream, otherwise it returns zero. also simple. also slay. ๐Ÿ’…

๐Ÿšซ ERRORS ๐Ÿšซ

these functions should not fail, and they do not set errno. like, at all. they're too chill for that. ๐Ÿ˜Œ


๐Ÿ”ฅ ATTRIBUTES ๐Ÿ”ฅ

for an explanation of the terms used in this section, go peek at attributes(7). (yes, we're citing our sources, we're academic like that ๐Ÿ“–)

so the interface here is clearerr(), feof(), ferror() โ€” and its attribute:

  • Thread safety: MT-Safe ๐Ÿงตโœจ

that's the whole table, bestie. one row, one vibe: safe to use across threads. no cap. ๐Ÿ”ฅ


๐Ÿ“ STANDARDS ๐Ÿ“

C11, POSIX.1-2008. she's certified. she's compliant. she passed the vibe check. โœ…


๐Ÿ•ฐ๏ธ HISTORY ๐Ÿ•ฐ๏ธ

C89, POSIX.1-2001. she's been around since before you were born and she's still serving. a true legacy icon. ๐Ÿ‘‘


๐Ÿ“ NOTES ๐Ÿ“

POSIX.1-2008 specifies that these functions shall not change the value of errno if stream is valid. so if your stream is valid, your errno stays exactly where you left her. untouched. unbothered. ๐Ÿ’…


โš ๏ธ CAVEATS โš ๏ธ

okay this one actually matters so listen up bestie, no scrolling past this one. ๐Ÿ›‘

normally, programs should read the return value of an input function, like fgetc(3), before using functions from the feof(3) family. like, read the actual result first. do not come for feof() before you've even asked the input function what happened.

only when that function returned the sentinel value EOF does it make sense to distinguish between the end of a file or an error, using feof(3) or ferror(3).

so the order is: call the input function โ†’ get EOF โ†’ then ask feof() or ferror() what's up. checking too early is giving main character energy in the wrong way. ๐Ÿ™ˆ


๐Ÿ‘€ SEE ALSO ๐Ÿ‘€

go read them. they're all part of the family. ๐ŸŒˆ


and that's the whole page, bestie!! ๐ŸŽ€ before you go: this was Linux man-pages 6.20, dated 2026-02-08, and it's page ferror(3) in case you need to cite her. mwah, go read your streams properly, ily ๐Ÿ’–๐Ÿฆ‹โœจ

๐Ÿ“ฆ where this page comes from
  • arch rolling: man-pages 6.20-1 ยท BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
  • nixpkgs unstable: man-pages 6.20
  • ๐Ÿ“œ the page's license: BSD-4-Clause-UC
  • ๐Ÿ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms