wctomb() Function
The wctomb()
function is declared in the header file <stdlib.h>
.
The wctomb()
function converts a wide character to its corresponding multibyte sequence. It stores the converted sequence in the provided array and returns the number of bytes used. This function maintains its own internal shift state which can be reset by calling it with a null pointer. The behavior of this function is dependent on the LC_CTYPE category of the current C locale.
Syntax of wctomb()
int wctomb(char *pmb, wchar_t wc);
Parameters
Parameter | Description |
---|---|
pmb | Pointer to an array large enough to hold a multibyte sequence (up to MB_CUR_MAX bytes). If pmb is null, the function resets its internal shift state. |
wc | A wide character of type wchar_t to be converted. |
It is worth noting that when pmb
is passed as a null pointer, wctomb()
resets its internal shift state and returns whether the current encoding is state-dependent.
Return Value
If pmb
is not null, the function returns the number of bytes that make up the converted multibyte sequence, or -1 if the conversion fails. If pmb
is null, it returns a nonzero value if the multibyte encoding is state-dependent, and zero otherwise.
Examples for wctomb()
Example 1: Converting a Wide Character to Multibyte Sequence
This example demonstrates how to convert a wide character to its multibyte representation and display the result.
Program
#include <stdio.h>
#include <stdlib.h>
#include <wchar.h>
#include <locale.h>
int main() {
// Set the locale to the default environment locale
setlocale(LC_CTYPE, "");
wchar_t wc = L'Ä';
char mbstr[MB_CUR_MAX];
int bytes = wctomb(mbstr, wc);
if (bytes == -1) {
printf("Conversion failed.\n");
} else {
mbstr[bytes] = '\0'; // Null-terminate the multibyte string
printf("Multibyte sequence: %s\n", mbstr);
printf("Number of bytes: %d\n", bytes);
}
return 0;
}
Explanation:
- The locale is set using
setlocale()
to ensure proper conversion based on the current environment. - A wide character
wc
is defined and initialized with a value (in this case,L'Ä'
). - An array
mbstr
of sizeMB_CUR_MAX
is declared to hold the multibyte sequence. wctomb()
converts the wide character to its multibyte sequence and returns the number of bytes used.- The multibyte string is null-terminated and printed along with the number of bytes.
Program Output:
Multibyte sequence: Ä
Number of bytes: 2
Example 2: Resetting the Internal Shift State
This example shows how to reset the internal shift state of wctomb()
by passing a null pointer as the destination.
Program
#include <stdio.h>
#include <stdlib.h>
#include <wchar.h>
int main() {
// Resetting the internal shift state
int state = wctomb(NULL, L'\0');
if (state == 0) {
printf("Multibyte sequences are not state-dependent.\n");
} else {
printf("Multibyte sequences are state-dependent.\n");
}
return 0;
}
Explanation:
- The program calls
wctomb()
with a null pointer to reset its internal shift state. - The return value indicates whether the multibyte encoding is state-dependent.
- A message is printed based on the returned value.
Program Output:
Multibyte sequences are not state-dependent.