C++ btowc()

The btowc() function in C++ converts a character to its wide character equivalent.

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

btowc() prototype

wint_t btowc( int c );

The btowc() function takes a single byte character c as its argument and returns its wide character equivalent.


btowc() Parameters

  • c: The single byte character to convert to wide character.

btowc() Return value

The btowc() function returns the wide character representation of c if c is a valid single byte character. If c is EOF, WEOF is returned.


Example: How btowc() function works?

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

int main()
{
	char str[] = "Hello\xf4\xdf";
	wchar_t wc;
	int count = 0;
	
	for (int i=0; i<strlen(str); i++)
	{
		wc = btowc(str[i]);
		if (wc != WEOF)
		count++;
	}
	
	cout << count << " out of " << strlen(str) << " characters were successfully widened";
	return 0;
}

When you run the program, the output will be:

5 out of 7 characters were successfully widened
Did you find this article helpful?