What is getwchar in C?

The getwchar function

The getwchar function is equivalent to the getch function in C. The only difference between the two is that getwchar works on wide characterssame as character data type, but take either 2 or 4 bytes space, whereas character data type takes 1 byte space, whereas getch works on normal characters.

The structure of the getwchar function is as follows:

wint_t getwchar( void );

The function getwchar reads a wide character from the standard input. If the sequence of bytes cannot be interpreted as a wide character, then the function returns the value WEOF.

Note: The reason that getwchar returns wint_t rather than wchar_t is to accommodate the special value WEOF.

To use the getwchar function, you need to include the wchar.h standard library.

Example usage of the getwchar function

The following example reads a character from the standard input and outputs it onto the console, unless x is the input:

Note: You need to provide an input in the STDIN of the following code widget (e.g., abcx).

#include<stdio.h>
#include<wchar.h>
int main() {
wint_t wchar_in = L'a';
do {
printf("\nInput any character (or 'x' to terminate): \n");
wchar_in = getwchar();
putwchar(wchar_in);
} while(wchar_in != L'x');
return 0;
}

Enter the input below

New on Educative
Learn to Code
Learn any Language as a beginner
Develop a human edge in an AI powered world and learn to code with AI from our beginner friendly catalog
🏆 Leaderboard
Daily Coding Challenge
Solve a new coding challenge every day and climb the leaderboard

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved