Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C

The <wchar.h> header — wide characters and WEOF

The <wchar.h> header provides wide-character and wide-string operations and multibyte conversions. WEOF signals conditions including the end of input.

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

Syntax

#include <wchar.h>

<wchar.h> declares input/output, wide-string handling, and multibyte conversion functions. It provides wchar_t, wint_t, and the conversion-state type mbstate_t.

WEOF is a constant expression of type wint_t that does not represent a character. It need not be negative or equal EOF. Store the result of fgetwc in wint_t, not wchar_t, before comparing it with WEOF. Since WEOF can mean end-of-file or an error, check feof and ferror.

The example reads an empty temporary file. The shown output assumes successful file operations. The displayed code was run unchanged in an isolated environment, and its standard output matched the stated output exactly.

Examples

#include <stdio.h>
#include <wchar.h>

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

    wint_t ch = fgetwc(stream);
    int ended = ch == WEOF && feof(stream) && !ferror(stream);

    if (fclose(stream) != 0) {
        return 1;
    }
    if (!ended) {
        return 1;
    }

    puts("end of file");
    return 0;
}

Result

end of file

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