mbstowcs() Function
The mbstowcs()
function is declared in the header file <stdlib.h>
.
The mbstowcs()
function converts a multibyte string into its equivalent wide-character string. It processes the input sequence until it either reaches a specified maximum number of wide characters or encounters a null character in the source string, converting the characters based on the current locale’s LC_CTYPE settings.
Syntax of mbstowcs()
size_t mbstowcs(wchar_t *dest, const char *src, size_t max);
Parameters
Parameter | Description |
---|---|
dest | Pointer to an array of wchar_t elements where the converted wide characters will be stored. The array should be large enough to hold at most max characters. |
src | C-string containing the multibyte characters to be converted. The multibyte sequence must begin in the initial shift state. |
max | Maximum number of wchar_t characters to be written to dest . |
The conversion behavior of mbstowcs()
is dependent on the LC_CTYPE category of the current C locale. If the function processes exactly max
characters, the resulting wide-character string in dest
is not null-terminated.
Return Value
The function returns the number of wide characters written to dest
, not including the terminating null character. If an invalid multibyte character is encountered during conversion, the function returns (size_t)-1
.
Examples for mbstowcs()
Example 1: Converting a Basic Multibyte String
This example demonstrates how to convert a simple multibyte string to a wide-character string using mbstowcs()
.
Program
#include <stdio.h>
#include <stdlib.h>
#include <wchar.h>
int main() {
const char *src = "Hello, World!";
wchar_t dest[50];
size_t ret;
ret = mbstowcs(dest, src, 50);
if (ret != (size_t)-1) {
dest[ret] = L'\0'; // Ensure null-termination
wprintf(L"Converted wide string: %ls\n", dest);
} else {
printf("Conversion error encountered.\n");
}
return 0;
}
Explanation:
- A multibyte string
"Hello, World!"
is defined. - A wide-character array
dest
is allocated to store the converted string. mbstowcs()
converts the multibyte string to wide characters, storing the result indest
.- The return value, representing the number of wide characters converted, is used to add a null terminator.
- The converted wide-character string is printed using
wprintf()
.
Program Output:
Converted wide string: Hello, World!
Example 2: Conversion with a Limited Buffer Size
This example shows how to convert a multibyte string when the destination buffer can only hold a limited number of wide characters. Note that if the limit is reached, the result may not be null-terminated.
Program
#include <stdio.h>
#include <stdlib.h>
#include <wchar.h>
int main() {
const char *src = "This is a longer multibyte string.";
wchar_t dest[10];
size_t ret;
ret = mbstowcs(dest, src, 9); // Leave space for manual null-termination
if (ret != (size_t)-1) {
dest[ret] = L'\0'; // Manual null-termination if possible
wprintf(L"Partially converted wide string: %ls\n", dest);
} else {
printf("Conversion error encountered.\n");
}
return 0;
}
Explanation:
- The multibyte string
src
is longer than the destination buffer size. - A destination array
dest
of limited size is allocated. mbstowcs()
is called with a maximum conversion limit, so only a portion of the string is converted.- A manual null terminator is added to ensure the wide-character string is properly terminated.
- The partially converted wide string is printed.
Program Output:
Partially converted wide string: This is a
Example 3: Handling an Invalid Multibyte Sequence
This example demonstrates how mbstowcs()
handles an invalid multibyte sequence by checking the return value for an error indicator.
Program
#include <stdio.h>
#include <stdlib.h>
#include <wchar.h>
int main() {
/* Assuming the source string contains an invalid multibyte sequence.
This might not be easily reproducible on all systems, but serves as an example. */
const char *src = "\xFF\xFFInvalid";
wchar_t dest[50];
size_t ret;
ret = mbstowcs(dest, src, 50);
if (ret == (size_t)-1) {
printf("Conversion error: Invalid multibyte sequence encountered.\n");
} else {
dest[ret] = L'\0';
wprintf(L"Converted wide string: %ls\n", dest);
}
return 0;
}
Explanation:
- An invalid multibyte sequence is provided in the source string.
- The
mbstowcs()
function attempts to convert the string. - If an invalid sequence is encountered, the function returns
(size_t)-1
. - The program checks the return value and prints an error message accordingly.
Program Output:
Conversion error: Invalid multibyte sequence encountered.