C library - mblen() function



The C stdlib library mblen() function is used to return the length in bytes of the multi-bytes character whose first byte is pointed to by 'str'. A multi-byte character is composed of sequences of one or more bytes.

The behaviour of mblen() depends on the current locale which consists of language and encoding parameters.

Syntax

Following is the C library syntax of the mblen() function −

int mblen(const char *str, size_t n)

Parameters

This function accepts following parameters −

  • str − This is the pointer to the first byte of a multi-byte character.

  • n − This is the maximum number of bytes to be checked for character length.

Return Value

This function returns following values −

  • If str is not a null pointer, returns the number of bytes passed from the multi-byte character starting at str.

  • If the first bytes pointed to by str do not form a valid multi-byte character, It returns -1.

  • If str is pointing at the null character. It returns 0.

Example 1

In this example, we create a basic c program to demonstrate the use of mblen() function.

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

int main() {
   char *mbstr = (char *)malloc(sizeof(char));
   int len;
   
   // first reset mblen
   mblen(NULL, 0);
   mbstr = "tutorialspoint";
   
   // display the length of a multibyte character
   len = mblen(mbstr, MB_CUR_MAX);
   printf("Length in bytes of multibyte character: %u\n", len);
   
   return 0;
}

Output

Following is the output −

Length in bytes of multibyte character: 1

Example 2

Let's create another example, we are getting the length of the multi-byte character using mblen() function. If the string is pointing at the null character.

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

int main() {
   char *mbstr = (char *)malloc(sizeof(char));
   int len;
   
   // first reset mblen
   mblen(NULL, 0);
   
   mbstr = NULL;
   
   // display the length of a multibyte character
   len = mblen(mbstr, MB_CUR_MAX);
   printf("Length in bytes of NULL multibyte character: %u\n", len);
   
   return 0;
}

Output

Following is the output −

Length in bytes of NULL multibyte character: 0

Example 3

The below c program gets the length of the multi-byte character and displaying the statement. If the string is invalid multi-byte sequence.

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

int main() {
	
   // create an invalid multi-byte sequence
   const char *invalidStr = "\xE2\x80";
   
   // Set locale for UTF-8
   setlocale(LC_ALL, "en_US.utf8"); 
   
   // get the lenfth of multi-byte character.
   int len = mblen(invalidStr, MB_CUR_MAX);
   
   if (len == -1) {
       perror("Invalid multi-byte sequence");
   } else {
       printf("Length in bytes of invalid multibyte character: %d\n", len);
   }
   return 0;
}

Output

Following is the output −

Invalid multi-byte sequence: Invalid or incomplete multibyte or wide character
Advertisements