w3resource

C mblen() function

C mblen() function - Get number of bytes in a character

Syntax mblen() function

int mblen(const char *str, size_t n)

The mblen() function is used to get the length in bytes of the multibyte character pointed to by string. n represents the maximum number of bytes examined.

Parameters mblen() function

Name Description Required /Optional
str Address of a multibyte-character byte sequence. Required
n This is the maximum number of bytes to be checked for character length. Required

Return value from mblen()

If string is NULL, the mblen() function returns:

  • A non-zero value indicates that the active locale supports mixed-byte strings.The function initializes the state variable.
  • Zero otherwise.

If string is not NULL, mblen() returns:

  • A non-zero value indicates that the active locale supports mixed-byte strings. The function initializes the state variable.
  • Zero otherwise.

Example: mblen() function

The following example shows the usage of mblen() function.

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

int main( void )
{
    int      i;
    char    *mbstr = (char *)malloc( sizeof( char ) );

    i = mblen( mbstr, MB_CUR_MAX );
    printf( "Length in bytes of multibyte character %x: %u\n", *mbstr, i );

    mbstr = NULL;
    i = mblen( mbstr, MB_CUR_MAX );
    printf( "Length in bytes of NULL multibyte character %x: %u\n", mbstr, i );
}

Output:

Length in bytes of multibyte character ffffff80: 1
Length in bytes of NULL multibyte character 0: 0

C Programming Code Editor:

Previous C Programming: C ldiv()
Next C Programming: C mbstowcs()



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/c-programming/stdlib/c-mblen.php