Standards / Extensions | C or C++ | Dependencies |
---|---|---|
ISO C |
both |
#include <stdlib.h>
int mbtowc(wchar_t * __restrict__pwc, const char* __restrict__string, size_t n);
Converts a multibyte character to a wide character and returns the number of bytes of the multibyte character. It first determines the length of the multibyte character pointed to by string. It then converts the multibyte character to the corresponding wide character and places the wide character in the location pointed to by pwc, if pwc is not a NULL pointer. A maximum of n bytes is examined.
The behavior of this wide-character function is affected by the LC_CTYPE category of the current locale. If you change the category, undefined results can occur.
If the current locale supports EBCDIC DBCS characters, the shift state is updated where applicable. The length returned may be up to 4 characters long (for the shift-out character, 2-byte code, and the shift-in character).
After the function is placed into its initial state, it interprets multibyte characters—pointed to by string—accordingly. During the processing of shift-dependent encoded characters, you cannot stop processing one string, then move temporarily to processing another string, and return to the first, because the state would be valid for the second string, not the place where you stopped in the first string.
/* This example uses mbtowc() to convert a multibyte character into a wide
character.
*/
#include <stdio.h>
#include <stdlib.h>
int temp;
char string [6];
wchar_t arr[6];
int main(void)
{ /* Set string to point to a multibyte character. */
⋮
temp = mbtowc(arr, string, MB_CUR_MAX);
printf("wide-character string: %ls",arr);
}