CKTIS / C
feof — C23 stream function
`feof(stream)` from `<stdio.h>` tests the end-of-file indicator belonging to an open `FILE` stream. It returns nonzero if and only if that indicator is set; it performs no read and does not return the `EOF` constant itself.
Version: C23
Syntax
int feof(FILE *stream);feof(stream) from <stdio.h> tests the end-of-file indicator belonging to an open FILE stream. It returns nonzero if and only if that indicator is set; it performs no read and does not return the EOF constant itself.
The example writes one character to a tmpfile(), rewinds it, reads the character, and observes feof == 0. Only the next fgetc returns EOF and sets the indicator; output 0 1 0 0 also shows no error and the state cleared by clearerr.
feof does not predict end of file: a loop must first test the input operation’s result, then use feof and ferror to diagnose why it stopped. Successfully reading the last byte does not yet set the indicator. stream must be a valid open stream; clearerr, rewind, and successful repositioning can clear the EOF state.
Examples
#include <stdio.h>
int main(void) {
FILE *stream = tmpfile();
if (stream == NULL) return 1;
if (fputc('A', stream) == EOF) { fclose(stream); return 1; }
rewind(stream);
if (fgetc(stream) != 'A') { fclose(stream); return 1; }
int before = feof(stream) != 0;
if (fgetc(stream) != EOF) { fclose(stream); return 1; }
int after = feof(stream) != 0;
int failed = ferror(stream) != 0;
clearerr(stream);
printf("%d %d %d %d\n", before, after, failed, feof(stream) != 0);
return fclose(stream) == 0 ? 0 : 1;
}
Result
0 1 0 0
The example writes one character to a `tmpfile()`, rewinds it, reads the character, and observes `feof == 0`. Only the next `fgetc` returns `EOF` and sets the indicator; output `0 1 0 0` also shows no error and the state cleared by `clearerr`.