Question Details

No question body available.

Tags

c file io standards libc

Answers (1)

March 21, 2026 Score: 2 Rep: 234,488 Quality: Medium Completeness: 100%

My question is, does the C standard guarantee that the ferror() indicator remains on unless I explicitly call clearerr()

The error indicator for a FILE stream is only cleared in one of two cases:

  • A call to clearerr
  • A call to rewind

So in your case, since you don't call rewind, you can be sure that the error indicator will be set if an error occurred at any time before you call ferror.

Section 7.23 of the C23 draft standard mentions the error indicator in several places, and only mentions that it cleared in these two places. It also mentions the function that may set it, specifically:

  • fflush
  • fgetc
  • fputc
  • getc
  • getchar
  • putc
  • putchar
  • fseek
  • fsetpos

While fprintf is not mentioned explicitly as a function that can set the error indicator, it is generally understood that uses one of the above internally that does.