C Function Mbtowc
[ C Standard Library - ](#)
`mbtowc()` is a function in the C standard library used to convert a multibyte character to a wide character.
The declaration for `mbtowc()` is located in the `` header file and is primarily used to support conversion between multibyte character sets (like UTF-8) and wide character sets (like Unicode).
`mbtowc()` is used to convert a multibyte character (`mbstr`) to a wide character (`wc`). The function interprets the multibyte character based on the current `LC_CTYPE` locale setting. If the multibyte character is valid, it is converted into a wide character. If the character is invalid, it returns `-1`.
### Declaration
Below is the declaration for the mbtowc() function.
#include int mbtowc(wchar_t *wc, const char *mbstr, size_t n);
**Parameters:**
* **wc**: A pointer to a `wchar_t` type variable. This variable will receive the converted wide character.
* **mbstr**: A pointer to the multibyte string, which is the sequence of multibyte characters to be converted.
* **n**: Specifies the maximum number of bytes in the `mbstr` string. Typically, you can set this to the length of the string.
### Return Value
* On success, `mbtowc()` returns the number of bytes in the converted character (i.e., the number of bytes of the multibyte character in `mbstr`). If `wc` is not `NULL`, this value is greater than 0.
* If an error occurs (e.g., an invalid multibyte character), the function returns `-1`.
* If the end of the character sequence is encountered (e.g., the null terminator `` of a string), it returns `0`.
## Example
The following example demonstrates the usage of the mbtowc() function.
## Example
#include
#include
#include
#include
#include
int main()
{
// Set the locale information for the program
setlocale(LC_CTYPE,"");
// Multibyte string
const char*str ="Here is .com";
// Wide character array to store the converted result
wchar_t mb;
// Current character index and the length of the converted wide character
int i =0;
int len;
// Convert characters to wide characters one by one
while(*str){
len = mbtowc(&mb, str, MB_CUR_MAX);// Convert the current multibyte character
if(len >0){
str += len;// Move to the start position of the next character
i++;// Store in the wide character array
}else if(len ==0){
break;// End of string terminator encountered
}else{
// Error handling for invalid characters
printf("Invalid charactern");
break;
}
}
// Output the converted wide character string
wprintf(L"%ls n", mb);
return 0;
}
Let's compile and run the above program. It will produce the following result, as it outputs the result in multibyte form, which is a binary output.
Here is .com
## Example
#include
#include
#include
int main(){
// Set the locale information for the program
setlocale(LC_CTYPE,"");
const char*mbstr ="Hello";
wchar_t wc;
// Convert characters one by one
for(size_t i =0; mbstr!=''; i++){
int len = mbtowc(&wc,&mbstr,1);
if(len >0){
wprintf(L"Converted wide character: %lcn", wc);
i += len -1;// Skip the number of bytes of the current multibyte character
}else if(len ==0){
break;
}else{
printf("Invalid charactern");
}
}
return 0;
}
**Explanation:**
1. **Multibyte Character Set**: In a multibyte character set (like UTF-8), a character may consist of multiple bytes, whereas in a wide character set (like Unicode), a character is typically represented by a single `wchar_t` value (which is 2 or 4 bytes on some platforms).
2. **Locale Setting**: The behavior of `mbtowc()` depends on the current program's locale setting, particularly the `LC_CTYPE` setting, which determines how multibyte character sets are interpreted. Therefore, when using this function, it is usually necessary to set an appropriate locale via `setlocale()`.
3. **Error Handling**: If the input multibyte string has an invalid format or cannot be converted to a wide character, `mbtowc()` returns `-1`. You need to check the return value to ensure the conversion was successful.
[ C Standard Library - ](#)
YouTip