6.19.24 mblen Function
Determines the length of a multibyte character.
Include
<wchar.h>
Prototype
int mblen(const char * s, size_t n);
Arguments
s
- a pointer to the multibyte character to check
n
- the maximum number of bytes to check
Return Value
If s
is a null pointer, the function returns a nonzero or zero value, which
indicates if multibyte character encodings, respectively, do or do not have state-dependent
encodings.
If s
is not a null pointer, the function returns 0 if s
points to the null character, returns −1 if the processed bytes do not form a valid multibyte
character, or returns the number of bytes of the converted multibyte character.
Remarks
The function determines the number of bytes taken up by the multibyte character pointed to by
s
.
Example
See the notes at the beginning of this chapter or section for
information on using printf()
or scanf()
(and other functions reading and writing the stdin
or
stdout
streams) in the example code.
#include <stdio.h>
#include <stdlib.h>
#include <locale.h>
int main(void)
{
int len;
char * mbs = "żoś";
setlocale(LC_CTYPE, "");
while(*mbs) {
len = mblen(mbs, 4);
printf("character length %d bytes\n", len);
mbs += len;
}
}
Example Output
character length 2 bytes
character length 1 bytes
character length 2 bytes