Standards / Extensions | C or C++ | Dependencies |
---|---|---|
ISO C Amendment |
both |
#include <wchar.h>
size_t mbrlen(const char * __restrict__s, size_t n, mbstate_t * __restrict__ps);
#define _XOPEN_SOURCE
#define _MSE_PROTOS
#include <wchar.h>
size_t mbrlen(const char *s, size_t n, mbstate_t *ps);
mbrtowc((wchar_t *)0, s, n, ps != NULL ? ps : &internal);
where &internal is the address of the internal mbstate_t object for the mbrlen() function.
mbrlen() is a restartable version of mblen(). That is, shift state information is passed as one of the arguments, and is updated on exit. With mbrlen(), you can switch from one multibyte string to another, provided that you have kept the shift-state information.
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.
Special behavior for XPG4
If you define any feature test macro specifying XPG4 behavior before the statement in your program source file to include the wchar header, then you must also define the _MSE_PROTOS feature test macro to make the declaration of the mbrlen() function in the wchar header available when you compile your program. Please see Table 1 for a list of XPG4 and other feature test macros.
If s is a NULL pointer, mbrlen() resets the shift state to the initial shift state and returns 0.
⁄* CELEBM03 *⁄
#include <stdio.h>
#include <stdlib.h>
#include <wchar.h>
int main(void)
{
char mbs[5] = "a"; ⁄* string containing the multibyte char *⁄
mbstate_t ss = 0; ⁄* set shift state to the initial state *⁄
int length;
⁄* Determine the length in bytes of a multibyte character pointed *⁄
⁄* to by mbs. *⁄
length = mbrlen(mbs, MB_CUR_MAX, &ss);
printf(" length: %d \n", length);
printf(" mbs:\"%s\"\n", mbs);
printf("MB_CUR_MAX: %d \n", MB_CUR_MAX);
printf(" ss: %d \n", ss);
}
length: 1
mbs:"a"
MB_CUR_MAX: 4
ss: 0