C Function Mblen
# C Library Function - mblen()
[ C Standard Library - ](#)
## Description
The C library function **int mblen(const char *str, size_t n)** returns the length of the multibyte character pointed to by the parameter **str**.
## Declaration
Below is the declaration for the mblen() function.
int mblen(const char *str, size_t n)
## Parameters
* **str** -- A pointer to the first byte of the multibyte character.
* **n** -- The maximum number of bytes to check for the character length.
## Return Value
If a non-empty wide character is recognized, the mblen() function returns the number of bytes parsed from the multibyte sequence starting at str. If an empty wide character is recognized, it returns 0. If an invalid multibyte sequence is recognized, or a complete multibyte character cannot be parsed, it returns -1.
## Example
The following example demonstrates the usage of the mblen() function.
#include #include #include int main(){ int len; char *pmbnull = NULL; char *pmb = (char *)malloc( MB_CUR_MAX ); wchar_t *pwc = L"Hi"; wchar_t *pwcs = (wchar_t *)malloc( sizeof( wchar_t )); printf("Convert to multibyte stringn"); len = wcstombs( pmb, pwc, MB_CUR_MAX); printf("Converted characters %dn", len); printf("Hexadecimal value of the first multibyte character: %#.4xn", pmb); len = mblen( pmb, MB_CUR_MAX ); printf( "Byte length of multibyte character %x: %un", pmb, len ); pmb = NULL; len = mblen( pmb, MB_CUR_MAX ); printf( "Byte length of multibyte character %x: %un", pmb, len ); return(0);}
Let us compile and run the above program, this will produce the following result:
Convert to multibyte stringConverted characters 1Hexadecimal value of the first multibyte character: 0x168c6010Byte length of multibyte character 168c6010: 1Byte length of multibyte character 0: 0
[ C Standard Library - ](#)
YouTip