C mblen() Function

Beginner
7 min read
Updated: Nov 2026
3 examples
<stdlib.h>

Definition

The C mblen() function measures one multibyte character. It examines at most max bytes using the current conversion state and locale. It does not write a wide character.

This function is declared in <stdlib.h>. Its contract matters as much as its call: validate every required precondition, check any documented sentinel, and keep ownership or host effects clear in the surrounding code. Success returns zero for the terminator or a positive byte count for one character. Failure for an invalid or incomplete sequence returns -1. The examples below move from the direct call to an edge condition and a practical use.

Syntax

Start with this concrete mblen() call:

C
mblen(text, MB_CUR_MAX);
CallWhat it does
mblen(text, MB_CUR_MAX);Measures one multibyte character using the concrete values shown.

Include <stdlib.h>. Also include headers that declare any types, limits, locale tools, or I/O functions used around the call.

Library declaration
C
int mblen(const char *str, size_t max);

Parameters

ParameterDescription
strPointer to a multibyte sequence, or NULL to query/reset conversion state.
maxMaximum bytes available to inspect.

Return Value

Return ValueDescription
Positive valueBytes in the next multibyte character.
0str points to the null character.
-1Invalid or incomplete sequence, or state-dependent encoding indication for a NULL query.

C Examples

Example 1 — Convert or measure basic text

This complete program demonstrates a distinct part of the mblen() contract and checks the result or precondition that matters for the scenario.

C
#include <limits.h>
#include <locale.h>
#include <stdio.h>
#include <stdlib.h>
#include <wchar.h>
int main(void){setlocale(LC_CTYPE,"C");int n=mblen("A",MB_CUR_MAX); printf("%d\n",n);return 0;}

How It Works

  1. Step 1. Select the portable C character locale for deterministic output.
  2. Step 2. Prepare the input and output objects.
  3. Step 3. Call mblen() with a byte or character limit.
  4. Step 4. Interpret its returned count under the selected locale.

Example 2 — Handle the terminator or size query

This complete program demonstrates a distinct part of the mblen() contract and checks the result or precondition that matters for the scenario.

C
#include <limits.h>
#include <locale.h>
#include <stdio.h>
#include <stdlib.h>
#include <wchar.h>
int main(void){setlocale(LC_CTYPE,"C");int n=mblen("",MB_CUR_MAX); printf("%d\n",n);return 0;}

How It Works

  1. Step 1. Select the portable C character locale for deterministic output.
  2. Step 2. Prepare the input and output objects.
  3. Step 3. Call mblen() with a byte or character limit.
  4. Step 4. Interpret its returned count under the selected locale.

Example 3 — Check a boundary or invalid sequence

This complete program demonstrates a distinct part of the mblen() contract and checks the result or precondition that matters for the scenario.

C
#include <limits.h>
#include <locale.h>
#include <stdio.h>
#include <stdlib.h>
#include <wchar.h>
int main(void){setlocale(LC_CTYPE,"C");const char bad[]={ (char)0xff, 0 }; printf("%s\n",mblen(bad,1)==-1?"invalid":"valid");return 0;}

How It Works

  1. Step 1. Select the portable C character locale for deterministic output.
  2. Step 2. Prepare the boundary case.
  3. Step 3. Call mblen() with a byte or character limit.
  4. Step 4. Interpret its sentinel or count before using output.

Frequently Asked Questions

mblen() measures one multibyte character. It examines at most max bytes using the current conversion state and locale. It does not write a wide character.
Include <stdlib.h> before calling mblen().
Success returns zero for the terminator or a positive byte count for one character. Failure for an invalid or incomplete sequence returns -1.
Interpretation depends on the current C locale. Call setlocale() when locale-aware behavior is intended, and never let max exceed the readable bytes.
The function is specified by the C standard library. Exact limits, locale behavior, environment behavior, allocation capacity, or host effects can still vary where relevant.

Next: mbstowcs()

Continue through the C stdlib.h tutorials.

mbstowcs() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful