C++ Standard Library C++ STL Library

C++ <cwchar> - wctob() Function



The C++ <cwchar> wctob() function narrows a wide character wc to its single-byte character equivalent only if wc represents a multibyte character with length 1 in initial shift state, otherwise returns EOF.

Syntax

int wctob (wint_t wc);        

Parameters

wc Specify the wide character to narrow.

Return Value

Returns single-byte character representation of wc if wc represents a multibyte character with length 1 in initial shift state, otherwise returns EOF.

Example:

The example below shows the usage of wctob() function.

#include <cwchar>
#include <cstdio>
 
void try_narrowing(wchar_t wc) {
  int c = wctob(wc);
  
  //EOF is used to check whether the widening of 
  //character was successful or not
  if(c != EOF)
    wprintf(L"Wide character %#x narrowed to %#x\n", wc, c);
  else
    wprintf(L"Wide character %#x failed to be narrowed\n", wc);
}

int main (){
  try_narrowing(L'A');
  try_narrowing(L'\t');
  try_narrowing(0x61);
  try_narrowing(0xe5b);
  return 0;
}

The output of the above code will be:

Wide character 0x41 narrowed to 0x41
Wide character 0x9 narrowed to 0x9
Wide character 0x61 narrowed to 0x61
Wide character 0xe5b failed to be narrowed

❮ C++ <cwchar> Library