C++ wctype()

The wctype() function in C++ returns a value of type wctype_t that is used for wide character classification.

The wctype() function is defined in <cwctype> header file.

wctype() prototype

wctype_t wctype(const char* str);

The wctype() function takes a C string str as its argument and returns a value of type wctype_t that is used for classifying a wide character.


wctype() Parameters

  • str: C string specifying the desired category.
Value of str for wctype
Value of str Equivalent function
alnum iswalnum
alpha iswalpha
blank iswblank
cntrl iswcntrl
digit iswdigit
graph iswgraph
lower iswlower
print iswprint
punct iswpunct
space iswspace
xdigit iswxdigit
upper iswupper

wctype() Return value

  • The wctype() function returns a wctype_t object that can be used with towctype() to check a wide character's property.
  • If str doesn't provide a category supported by the current C locale, it returns zero.

Example: How wctype() function works?

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

int main()
{
	setlocale(LC_ALL, "en_US.UTF-8");
	wchar_t wc = L'\u00b5';

	if (iswctype(wc, wctype("digit")))
		wcout << wc << L" is a digit";
	else if (iswctype(wc, wctype("alpha")))
		wcout << wc << L" is an alphabet";
	else
		wcout << wc << L" is neither an alphabet nor a digit";
		
	return 0;
}

When you run the program, the output will be:

µ is an alphabet