C++ getwchar()

The getwchar() function in C++ reads the next wide character from stdin.

The getwchar() function is defined in <cwchar> header file.

getwchar() prototype

wint_t getwchar();

The getwchar() function is equivalent to a call to getwc(stdin). It reads the next character from stdin which is usually the keyboard.


getwchar() Parameters

  • None.

getwchar() Return value

  • On success, the getwchar() function returns the entered wide character.
  • WEOF is returned if an error has occurred or end of file is reached.

Example: How getwchar() function works?

#include <iostream>
#include <cwchar>
#include <clocale>
using namespace std;

int main()
{
	int i=0;
	wchar_t c;
	wchar_t str[100];

	setlocale(LC_ALL, "en_US.UTF-8");
	wcout << L"Enter characters, Press Enter to stop\n";
	do
	{
		c = getwchar();
		str[i] = c;
		i++;
	}while(c!=L'\n');

	wcout << L"You entered : " << str;
	return 0;
}

When you run the program, a possible output will be:

Enter characters, Press Enter to stop
äs12 ɏ
You entered : äs12 ɏ
Did you find this article helpful?