C++ Standard Library C++ STL Library

C++ <cwchar> - btowc() Function



The C++ <cwchar> btowc() function widens a single-byte character c to its wide character equivalent only if (unsigned char)c is a valid single-byte character in the initial shift state, otherwise returns WEOF.

Syntax

wint_t btowc (int c);          

Parameters

c Specify the single-byte character to widen.

Return Value

Returns wide character representation of c if (unsigned char)c is a valid single-byte character in the initial shift state, otherwise returns WEOF. Returns WEOF if c is EOF.

Example:

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

#include <cwchar>
 
void try_widen(unsigned char c) {
  //wint_t type is used to hold the character 
  //value of the wide character set
  wint_t wc = btowc(c);
  
  //WEOF is used to check whether the widening of 
  //character was successful or not
  if(wc != WEOF)
    wprintf(L"Single-byte character %#x widens to %#x\n", c, wc);
  else
    wprintf(L"Single-byte character %#x failed to widen\n", c);
}

int main (){
  try_widen('A');
  try_widen('\t');
  try_widen(0xf9);
  try_widen(0xdf);
  return 0;
}

The output of the above code will be:

Single-byte character 0x41 widens to 0x41
Single-byte character 0x9 widens to 0x9
Single-byte character 0xf9 failed to widen
Single-byte character 0xdf failed to widen

❮ C++ <cwchar> Library