In C, mbsrtowcs() converts a multibyte string to a wide string. It belongs to the wide-character tools declared in <wchar.h>.
Use wchar_t for wide characters and prefix wide literals with L, as in L"Hello". The examples below use simple C Programming Tutorial patterns and print wide strings with %ls.
Syntax
The following concrete call shows the argument shape before the formal declaration.
C
mbsrtowcs(out, &src, 32, &state);
Call
What it does
mbsrtowcs(out, &src, 32, &state)
Converts a multibyte string to a wide string in a typical call.
Include <wchar.h>. Add only the supporting headers actually used by your program.
Destination buffer; NULL may request only the required length for conversion functions.
src
Source pointer; conversion functions update it as input is consumed.
len
Maximum destination elements or bytes to write.
ps
Conversion state; pass a zero-initialized mbstate_t object or NULL where the function permits it.
Return Value
Return Value
Description
size_t result
The converted length or element count.
(size_t)-1 / -2
Invalid or incomplete multibyte input, where supported by the function.
C Examples
Example 1 — Convert a basic value
This example uses mbsrtowcs() to convert a basic value, then exposes the result so you can connect the call with its output.
C
#include <stdio.h>
#include <wchar.h>
int main(void) {
mbstate_t state = {0}; const char *src = "Wide"; wchar_t out[16];
size_t n = mbsrtowcs(out, &src, 16, &state);
if (n != (size_t)-1) { out[n] = L'\0'; wprintf(L"%ls\n", out); }
return 0;
}
Output
Wide
How It Works
Set up this case. The program prepares the exact input needed to convert a basic value.
Run the conversion.mbsrtowcs(out, &src, 32, &state) processes the next value while preserving any conversion state.
Read the normal result. The displayed output shows the direct result of the call for this input.
Interpret the output.Wide confirms the behavior demonstrated by this example.
Example 2 — Check conversion status
This example uses mbsrtowcs() to check conversion status, then exposes the result so you can connect the call with its output.
C
#include <stdio.h>
#include <wchar.h>
int main(void) {
mbstate_t state = {0}; const char *src = "Wide"; wchar_t out[16];
size_t n = mbsrtowcs(out, &src, 16, &state);
if (n != (size_t)-1) { out[n] = L'\0'; wprintf(L"%ls\n", out); }
return 0;
}
Output
Wide
How It Works
Choose the edge case. This example changes the input so you can see how mbsrtowcs() behaves for check conversion status.
Run the conversion.mbsrtowcs(out, &src, 32, &state) processes the next value while preserving any conversion state.
Inspect the status or pointer. The program checks what the function returned before relying on output data.
Interpret the output.Wide confirms the behavior demonstrated by this example.
Example 3 — Use an initial state
This example uses mbsrtowcs() to use an initial state, then exposes the result so you can connect the call with its output.
C
#include <stdio.h>
#include <wchar.h>
int main(void) {
mbstate_t state = {0}; const char *src = "Wide"; wchar_t out[16];
size_t n = mbsrtowcs(out, &src, 16, &state);
if (n != (size_t)-1) { out[n] = L'\0'; wprintf(L"%ls\n", out); }
return 0;
}
Output
Wide
How It Works
Prepare a practical variation. The data is arranged specifically to use an initial state.
Run the conversion.mbsrtowcs(out, &src, 32, &state) processes the next value while preserving any conversion state.
Notice the safety rule. The example keeps the count, destination capacity, stream state, and terminator requirements visible.
Interpret the output.Wide confirms the behavior demonstrated by this example.
Frequently Asked Questions
mbsrtowcs() converts a multibyte string to a wide string. It is part of the standard wide-character library.
Its parameters are dst, src, len, ps. dst: Destination buffer; NULL may request only the required length for conversion functions. src: Source pointer; conversion functions update it as input is consumed. len: Maximum destination elements or bytes to write. ps: Conversion state; pass a zero-initialized mbstate_t object or NULL where the function permits it.
size_t result means the converted length or element count. (size_t)-1 / -2 means invalid or incomplete multibyte input, where supported by the function.
Include <wchar.h> for mbsrtowcs(). Include only the supporting headers used by your program, such as <stdio.h> for FILE or <stdarg.h> for va_list.
Conversion depends on the active C locale. Keep the mbstate_t object initialized and do not treat (size_t)-1 or (size_t)-2 as a normal length.