Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

C library function - mblen()

C library function - mblen()

C Standard Library - <stdlib.h> C Standard Library -

Description

C library function *int mblen(const char str, size_t n) Returns the length of the multibyte character pointed to by str the given parameter.

Declaration

Here is the declaration of the mblen() function.



int mblen(const char *str, size_t n)

Parameters

Return Value

If a non-null wide character is recognized, the mblen() function returns the number of bytes parsed from the multibyte sequence starting at str. If a null 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 use 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("转换为多字节字符串\n");

   len = wcstombs( pmb, pwc, MB_CUR_MAX);

   printf("被转换的字符 %d\n", len);

   printf("第一个多字节字符的十六进制值:%#.4x\n", pmb);

   

   len = mblen( pmb, MB_CUR_MAX );

   printf( "多字节字符 %x 的字节长度:%u\n", pmb, len );

   

   pmb = NULL;

   

   len = mblen( pmb, MB_CUR_MAX );

   printf( "多字节字符 %x 的字节长度:%u\n", pmb, len );

   

   return(0);

}

Let's compile and run the above program, which will produce the following result:



转换为多字节字符串

被转换的字符 1

第一个多字节字符的十六进制值:0x168c6010

多字节字符 168c6010 的字节长度:1

多字节字符 0 的字节长度:0

C Standard Library - <stdlib.h> C Standard Library -