The C wctomb() function converts one wide character to multibyte form. It writes the current locale's multibyte sequence for wc. Passing NULL queries whether the encoding is state-dependent and resets internal state.
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 bytes written for the character. Failure returns -1 because wc has no valid representation in the active locale. The examples below move from the direct call to an edge condition and a practical use.
Syntax
Start with this concrete wctomb() call:
C
wctomb(bytes, L'Z');
Call
What it does
wctomb(bytes, L'Z');
Converts one wide character to multibyte form 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 wctomb(char *dest, wchar_t wc);
Parameters
Parameter
Description
dest
Output byte array large enough for MB_CUR_MAX bytes, or NULL to query/reset state.
wc
Wide character to convert.
Return Value
Return Value
Description
Positive value
Bytes written for a non-null wide character.
1 or implementation result
Bytes used for the null character.
-1
The character is not representable in the current locale.
C Examples
Example 1 — Convert or measure basic text
This complete program demonstrates a distinct part of the wctomb() 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");char out[MB_LEN_MAX]; int n=wctomb(out,L'A'); printf("%d %c\n",n,out[0]);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 wctomb() 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 wctomb() 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");char out[MB_LEN_MAX]; int n=wctomb(out,L'\0'); printf("bytes=%d\n",n);return 0;}
Output
bytes=1
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 wctomb() with a byte or character limit.
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 wctomb() 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 wctomb() with a byte or character limit.
Step 4. Interpret its sentinel or count before using output.
Frequently Asked Questions
wctomb() converts one wide character to multibyte form. It writes the current locale's multibyte sequence for wc. Passing NULL queries whether the encoding is state-dependent and resets internal state.
Include <stdlib.h> before calling wctomb().
Success returns the number of bytes written for the character. Failure returns -1 because wc has no valid representation in the active locale.
The output encoding comes from the current locale. Provide at least MB_CUR_MAX writable bytes and handle -1 before reading the buffer.
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.