C mbtowc() Function

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

Definition

The C mbtowc() function converts one multibyte character to wchar_t. At most max bytes are inspected. On a successful non-null conversion, the resulting wide character is stored through dest when dest is not NULL.

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 number of consumed bytes. Failure returns -1 and the output must not be used. The examples below move from the direct call to an edge condition and a practical use.

Syntax

Start with this concrete mbtowc() call:

C
mbtowc(&wc, text, MB_CUR_MAX);
CallWhat it does
mbtowc(&wc, text, MB_CUR_MAX);Converts one multibyte character to wchar_t 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 mbtowc(wchar_t *dest, const char *src, size_t max);

Parameters

ParameterDescription
destWide-character output pointer, or NULL when output is not needed.
srcMultibyte sequence, or NULL to query/reset state.
maxMaximum readable bytes.

Return Value

Return ValueDescription
Positive valueBytes consumed.
0The sequence represents the null wide character.
-1Invalid or incomplete sequence.

C Examples

Example 1 — Convert or measure basic text

This complete program demonstrates a distinct part of the mbtowc() 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");wchar_t wc; int n=mbtowc(&wc,"A",MB_CUR_MAX); printf("%d %lc\n",n,wc);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 mbtowc() 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 mbtowc() 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");wchar_t wc; printf("%d\n",mbtowc(&wc,"",MB_CUR_MAX));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 mbtowc() 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 mbtowc() 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 }; wchar_t wc; puts(mbtowc(&wc,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 mbtowc() with a byte or character limit.
  4. Step 4. Interpret its sentinel or count before using output.

Frequently Asked Questions

mbtowc() converts one multibyte character to wchar_t. At most max bytes are inspected. On a successful non-null conversion, the resulting wide character is stored through dest when dest is not NULL.
Include <stdlib.h> before calling mbtowc().
Success returns zero for the terminator or a positive number of consumed bytes. Failure returns -1 and the output must not be used.
The active locale controls the encoding. Keep max within the actual buffer and handle -1 before using the output.
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: qsort()

Continue through the C stdlib.h tutorials.

qsort() 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