Javatpoint Logo
Javatpoint Logo

btowc() function in C/C++

In this article, we will discuss btowc() function in C/C++ with its syntax and examples.

The btown() function is a standard library function in C, and it converts a single-byte character to a wide character. It is used to convert a single-byte character to a corresponding wide character, taking into account the current locale settings.

Functionality of btowc() function:

The basic functionality of btowc() function is to take a single-byte character as an argument and return its equivalent wide character.

The syntax of btowc() function in C language:

It has the following syntax:

Here 'c' is the single-byte character to be converted.

If 'c' equals the 'EOF' constant or is not a valid single-byte character, the function returns 'WEOF', typically represented as '-1' or a value indicating an invalid wide character. Otherwise, it returns the wide-character equivalent of the single-byte character.

This function is mainly used in character conversion, such as reading characters from a file or when working with multibyte character encodings. It helps bridge the gap between single-byte and wide characters, facilitating the handling of a broader range of characters in C programming.

Example:

Let us take a program to illustrate the btowc() function in C:

Output:

btowc() function in C/C++

Explanation:

In this example, the program converts each character from a single-byte character string "I love India" to its comprehensive character representation using the btowc() function. After that, it prints information about the conversion, indicating whether it was successful or if a failure wide character is a space, and it provides additional information about it.

For each character, this program converts each character to its wide character representation using the btwoc() function. After that, the program prints data about the conversion. Indicating success or failure. In this case, an additional message is printed of a successful conversion, resulting in a wide character being a space. If the conversion fails, the program will print the error message, assign a default wide character, and provide the information about the default character.

Example:

Let us take a C++ program to illustrate the btowc() function:

Output:

btowc() function in C/C++

Explanation:

In this example, the program demonstrates how characters are represented in different encodings. Encodings are like other languages for computers to store text. The program tests how certain characters are represented in two encodings: Lithuanian ISO-8859-4 and Lithuanian UTF-8.

The program sets the locale to Lithuanian ISO-8859-4 and then tests how the characters 'A', '\xdf', and '\xf9' are widened to wide characters. Widening a character means converting it from a single-byte to a multiple-byte representation.

After that, the program sets the locale to Lithuanian UTF-8 and repeats the same test. It shows how the representation of characters can change depending on the encoding being used.

Conclusion:

In conclusion, the btowc() function in C and C++ plays a crucial role in character conversion, bridging the gap between single-byte and wide characters. Its primary functionality involves converting a single-byte character to its wide-character equivalent. The syntax of the function is standardized, and it returns the wide character or WEOF in case of conversion failure. The provided C program exemplifies the practical use of btowc() by converting characters from a single-byte string to their wide character representations and providing informative output. Additionally, the C++ version extends this exploration by demonstrating character widening in different locales, emphasizing the impact of encodings on character representation.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA