Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C

EOF — end-of-file or error sentinel

EOF is a macro from <stdio.h>, used by functions such as fgetc to signal end-of-file or a read error.

Version: C23; repository compiler flag, production runtime not attested

Syntax

#include <stdio.h>
EOF

EOF from <stdio.h> expands to an integer constant expression of type int with a negative value, not necessarily -1. It is not a character stored in a file.

Store the result of fgetc in int, not char, and compare it with EOF before conversion. EOF can mean end-of-file or a read error; check feof(stream) and ferror(stream) after reading. Do not use feof to predict whether the next read will succeed.

Examples

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
    FILE *stream = tmpfile();
    if (stream == NULL) {
        return EXIT_FAILURE;
    }

    int ch = fgetc(stream);
    printf("EOF=%d end=%d error=%d\n",
           ch == EOF, feof(stream) != 0, ferror(stream) != 0);

    return fclose(stream) == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
}

Result

EOF=1 end=1 error=0

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources