C mbrtowc() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
wchar.h

Definition

In C, mbrtowc() converts the next multibyte character to wchar_t. 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
mbrtowc(&wc, "A", 1, &state);
CallWhat it does
mbrtowc(&wc, "A", 1, &state)Converts the next multibyte character to wchar_t in a typical call.

Include <wchar.h>. Add only the supporting headers actually used by your program.

Library declaration
size_t mbrtowc(wchar_t *restrict pwc, const char *restrict s, size_t n, mbstate_t *restrict ps);

Parameters

ParameterDescription
pwcDestination that receives the converted wide character.
sA multibyte input pointer or output byte buffer.
nThe buffer capacity or maximum number of elements to process.
psConversion state; pass a zero-initialized mbstate_t object or NULL where the function permits it.

Return Value

Return ValueDescription
size_t resultThe converted length or element count.
(size_t)-1 / -2Invalid or incomplete multibyte input, where supported by the function.

C Examples

Example 1 — Convert a basic value

This example uses mbrtowc() 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}; wchar_t wc;
    size_t n = mbrtowc(&wc, "A", 1, &state);
    wprintf(L"%lc uses %zu byte\n", wc, n);
    return 0;
}

How It Works

  1. Set up this case. The program prepares the exact input needed to convert a basic value.
  2. Run the conversion. mbrtowc(&wc, "A", 1, &state) processes the next value while preserving any conversion state.
  3. Read the normal result. The displayed output shows the direct result of the call for this input.
  4. Interpret the output. A uses 1 byte confirms the behavior demonstrated by this example.

Example 2 — Check conversion status

This example uses mbrtowc() 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}; wchar_t wc;
    size_t n = mbrtowc(&wc, "A", 1, &state);
    wprintf(L"%lc uses %zu byte\n", wc, n);
    return 0;
}

How It Works

  1. Choose the edge case. This example changes the input so you can see how mbrtowc() behaves for check conversion status.
  2. Run the conversion. mbrtowc(&wc, "A", 1, &state) processes the next value while preserving any conversion state.
  3. Inspect the status or pointer. The program checks what the function returned before relying on output data.
  4. Interpret the output. A uses 1 byte confirms the behavior demonstrated by this example.

Example 3 — Use an initial state

This example uses mbrtowc() 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}; wchar_t wc;
    size_t n = mbrtowc(&wc, "A", 1, &state);
    wprintf(L"%lc uses %zu byte\n", wc, n);
    return 0;
}

How It Works

  1. Prepare a practical variation. The data is arranged specifically to use an initial state.
  2. Run the conversion. mbrtowc(&wc, "A", 1, &state) processes the next value while preserving any conversion state.
  3. Notice the safety rule. The example keeps the count, destination capacity, stream state, and terminator requirements visible.
  4. Interpret the output. A uses 1 byte confirms the behavior demonstrated by this example.

Frequently Asked Questions

mbrtowc() converts the next multibyte character to wchar_t. It is part of the standard wide-character library.
Its parameters are pwc, s, n, ps. pwc: Destination that receives the converted wide character. s: A multibyte input pointer or output byte buffer. n: The buffer capacity or maximum number of elements to process. 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 mbrtowc(). 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.

Next: C mbsinit() Function

Continue learning C wide-character functions.

Next 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