btowc() function in C/C++17 Mar 2025 | 4 min read 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: ![]() 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: ![]() 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. Next TopicC++ program for S?ntin?l Lin?ar S?arch |
Insertion sort is a comparison-based sorting algorithm that builds the final sorted array one element at a time. It works by dividing the input array into two regions: a sorted region and an unsorted region. Initially, the sorted region consists of only the first element, and...
9 min read
Copy elision is defined as an optimisation technique that is used to avoid the unnecessary copying of objects. Generally, all the compilers use the technique of copy elision. The optimisation technique is not available to a temporary object that is bound to a reference. It is also...
1 min read
In this article, we will discuss the C++ program for iterative quick sort. But before going to its implementation, we must know about the iterative quick sort with its algorithm and example. One popular sorting algorithm well-known for its practical efficiency and efficacy is called 'Quick Sort'....
4 min read
In this article, we will discuss how to find the maximum product subarray in C++. Find the largest product of the subarray of positive and negative integers in the given array. O(n) is the predicted time complexity, and the only usable extra space is O(1). Examples: Input: arr[] =...
3 min read
The foreach loop is used to quickly iterate over the elements of a container (array, vectors, etc.) without performing initialization, testing, or increment/decrement. Foreach loops work by doing something for each element rather than doing something n times. Although there is no foreach loop in C,...
4 min read
In this article, you will learn about the call by value in C++ with its mechanism, examples, advantages, and disadvantages. Introduction to Call by Value Passing the data to functions or methods is a common thing while programming. It allows you to modularize your code, reuse functionality, and...
5 min read
In addition to using pointers to modify memory addresses directly, C++ provides a robust set of memory management capabilities. While pointers are necessary for dynamic memory allocation, improper management can lead to problems like memory leaks and unpredictable behavior. Unique_ptr is a crucial part of the...
3 min read
In this article, we will discuss the difference between the array::fill() and array::swap() in C++. But before discussing their differences, we must know about the array::fill() and array::swap(). Two member functions of the C++ Standard Template Library (STL) that are connected to the std::array template class are...
4 min read
In the realm of C++ programming, grappling with determining maximum and minimum values for different integral data types can pose quite a challenge. Fortunately, the climits library occurs as a lifesaver, also denoted as limits.h in C++. This library introduces a collection of macros that precisely...
3 min read
In C++, the ungetc() function is used to push a character back into the input stream. This function is part of the Standard Input/Output Library and is typically used with file input streams (FILE* streams). The is part of the Standard Input/Output Library and is used...
14 min read
We request you to subscribe our newsletter for upcoming updates.

We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India

