Join our newsletter for the latest updates.

C++ mbtowc()

The mbtowc() function in C++ converts a multibyte character to a wide character.

The mbtwoc() function is defined in <cstdlib> header file.

mbtowc() prototype

int mbtowc (wchar_t* pwc, const char* pmb, size_t max);

The mbtowc() function takes three arguments and returns an integer value. This function converts the multibyte character pointed by pmb to a wide character (value of type wchar_t) and is stored at the memory location pointed by pwc.

If pmb is a null pointer, a call to mbtowc() will reset the global conversion state and determines whether shift sequences are used.

mbtowc() Parameters

  • pwc: Pointer to the resulting wide character
  • pmb: Pointer to the multibyte character which is converted to wide character
  • max: Maximum size in bytes of pmb to consider for the multibyte character.

mbtowc() Return value

If pmb is not a null pointer, mbtowc() returns:

  • the number of bytes that are contained in the multibyte character pointed by pmb.
  • -1 if the first byte pointed by pmb do not form a valid multibyte character.
  • 0 if pmb is pointing to the terminating null character i.e. '\0'.

If pmb is a null pointer, resets its internal conversion state to represent the initial shift state and returns:

  • 0 if the current multibyte encoding is not state-dependent (does not use shift sequences)
  • a non-zero value if the current multibyte encoding is state-dependent (uses shift sequences).

Example : How mbtowc() function works?

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

int main()
	char pmb[] = "Welcome to";
	wchar_t pwc[100];
	int len, ret_val;

	/* resets internal conversion state */
	mbtowc (NULL, NULL, 0);
	len = strlen(pmb);
	ret_val = mbtowc(pwc, pmb, strlen(pmb));

	cout << "Return Value = " << ret_val << endl;
	wcout << "Wide character string: " << pwc;


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

Return Value = 1
Wide character string: W@