C Reference function mblen()

The function mlen() of the library stdlib.h is used to get the length of a multi-byte character.

Usage of mblen():

int mblen ( const char * pmb, size_t max );

The pmb (pointer multibyte character) pointer is pointing to the multi-byte value where you want to know the size of. It will only examine at most max bytes.


The pmb pointer is used to point to the first byte of a multi-byte character. The max parameter is the maximum number of bytes to be checked.

Return value:

If pmb is NULL, the mblen() function returns a non-zero if the active locale allows mixed-byte strings.
The function initializes the state variable. Otherwise a zero is returned.

If string is not NULL, mblen() returns a zero if pmb points to the null character, the number of bytes comprising the multibyte character or -1 if pmb does not point to a valid multi-byte character.

Source code example of mblen():

The example uses the functions mblen() and mbtowc().
It will convert a multi-byte character to single wide character.


	int length, temp;
	char strdata [6] = "m";
	wchar_t my_array[6];

	int main(void)
		/* initialize internal state variable */
		length = mblen(NULL, MB_CUR_MAX);

		/* set strdata to point to a multibyte character  */
		length = mblen(strdata, MB_CUR_MAX);

		temp = mbtowc(arr,strdata,length);
		my_array[1] = L'\0';
		printf("wide character strdata: %ls\n", my_array);

This entry was posted in C Reference stdlib.h Functions. You can follow any responses to this entry through the RSS 2.0 feed. Both comments and pings are currently closed. Tweet This! Tweet This! or use to share this post with others.

Comments are closed.