What is fgetwc in C ?

The fgetwc function reads a wide character from stream and returns it. To use this function, the wchar.h header file needs to be included as shown below:

#include <wchar.h>

Prototype

This function is written as shown below:

wint_t fgetwc( FILE *stream );

Parameters

stream - pointer to a FILE that identifies a stream from which the character is read.

Return value

This function returns the wide-character that is read from the stream as type wint_t in case of success. Otherwise, it returns WEOF. WEOF indicated end-of-file or some other error.

Example

The following code shows the usage of the fgetwc function. It reads a file sampleFile.txt , stores its content in wideChar using fgetwc, and counts the number of a letters in the file until the counter n reaches the end of the file:

main.c
sampleFile.txt
#include <stdio.h>
#include <wchar.h>
int main ()
{
FILE * thisFile;
wint_t wideChar;
int n = 0;
thisFile=fopen ("sampleFile.txt","r");
if (thisFile!=NULL)
{
do {
wideChar = fgetwc (thisFile);
if (wideChar == L'a') n++;
} while (wideChar != WEOF);
fclose (thisFile);
wprintf (L"The file contains %d a letters ($).\n",n);
}
return 0;
}

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved