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);
Call
What 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
Parameter
Description
dest
Wide-character output pointer, or NULL when output is not needed.
src
Multibyte sequence, or NULL to query/reset state.
max
Maximum readable bytes.
Return Value
Return Value
Description
Positive value
Bytes consumed.
0
The sequence represents the null wide character.
-1
Invalid 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;}
Output
1 A
How It Works
Step 1. Select the portable C character locale for deterministic output.
Step 2. Prepare the input and output objects.
Step 3. Call mbtowc() with a byte or character limit.
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.
Step 1. Select the portable C character locale for deterministic output.
Step 2. Prepare the boundary case.
Step 3. Call mbtowc() with a byte or character limit.
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.