C mbstowcs() Function

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

Definition

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);
CallWhat 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.

Library declaration
C
size_t mbstowcs(wchar_t *dest, const char *src, size_t count);

Parameters

ParameterDescription
destDestination wide-character array, or NULL for a length query.
srcNull-terminated multibyte string.
countMaximum wide characters to store.

Return Value

Return ValueDescription
CountWide characters converted, excluding the terminator.
(size_t)-1An invalid multibyte sequence was encountered.

C Examples

Example 1 — Convert or measure basic text

This complete program demonstrates a distinct part of the mbstowcs() 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 out[16]; size_t n=mbstowcs(out,"Hello",16); printf("%zu %lc\n",n,out[0]);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 mbstowcs() 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 mbstowcs() 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");size_t n=mbstowcs(NULL,"Hello",0); printf("needed=%zu\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 mbstowcs() 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 mbstowcs() 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 out[3]; size_t n=mbstowcs(out,"Hello",3); printf("converted=%zu\n",n);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 mbstowcs() with a byte or character limit.
  4. 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.

Next: mbtowc()

Continue through the C stdlib.h tutorials.

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