Latest web development tutorials

C library functions - mblen ()

C standard library - <stdlib.h> C standard library - <stdlib.h>

description

C library functionsint mblen (const char * str, size_t n) returns the length argument strpoints to multi-byte characters.

statement

Here is mblen () function's declaration.

int mblen(const char *str, size_t n)

parameter

  • str - points to the first byte of multi-byte character pointer.
  • The maximum number of bytes to check the character length- n.

return value

If the identification of a non-null wide character, mblen () function returns the number of bytes of multi-byte sequence str start parsing. If you identify a null wide character, it returns 0. If you identify an invalid multi-byte sequence, or can not parse a complete multibyte character, -1 is returned.

Examples

The following example demonstrates mblen () function is used.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

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 result in the following:

转换为多字节字符串
被转换的字符 1
第一个多字节字符的十六进制值:0x168c6010
多字节字符 168c6010 的字节长度:1
多字节字符 0 的字节长度:0

C standard library - <stdlib.h> C standard library - <stdlib.h>