The C mbstowcs() function converts a multibyte string to wide characters. It converts sequential characters as if by repeated mbtowc calls in an initial conversion state. A NULL destination can be used to measure the required count.
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 the number of converted wide characters, excluding the terminator. Failure returns (size_t)-1; do not treat it as a very large successful length. The examples below move from the direct call to an edge condition and a practical use.
Syntax
Start with this concrete mbstowcs() call:
C
mbstowcs(wide, text, 32);
Call
What it does
mbstowcs(wide, text, 32);
Converts a multibyte string to wide characters 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.
Step 1. Select the portable C character locale for deterministic output.
Step 2. Prepare the boundary case.
Step 3. Call mbstowcs() with a byte or character limit.
Step 4. Interpret its sentinel or count before using output.
Frequently Asked Questions
mbstowcs() converts a multibyte string to wide characters. It converts sequential characters as if by repeated mbtowc calls in an initial conversion state. A NULL destination can be used to measure the required count.
Include <stdlib.h> before calling mbstowcs().
Success returns the number of converted wide characters, excluding the terminator. Failure returns (size_t)-1; do not treat it as a very large successful length.
Conversion follows the current locale. If count fills before the source terminator, the destination is not null-terminated.
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.