The btowc() is a built-in function in C/C++ which converts a character to its wide character equivalent. It is defined within the cwchar header file of C++.
Syntax:
wint_t btowc( int ch );
Parameter:The function accepts a single mandatory parameter ch which specifies to the single byte character to convert to it’s wide character.
Return Value:
The function returns the wide character representation of ch if ch is a valid single byte character. If ch is EOF, WEOF is returned.
Below programs illustrates the above function.
Program 1:
// Program to illustrate // btowc() function #include <cstring> #include <cwchar> #include <iostream> using namespace std; int main() { char str[] = "Geeksfor\xf4\xdgeeks"; wchar_t wc; int count = 0; for (int i = 0; i < strlen(str); i++) { wc = btowc(str[i]); if (wc != WEOF) count++; } cout << count << " out of " << strlen(str) << " Characters were successfully widened"; return 0; } |
14 out of 15 Characters were successfully widened
Program 2:
// Program to illustrate // btowc() function #include <cstring> #include <cwchar> #include <iostream> using namespace std; int main() { char str[] = "Ishwar\xa1\xcGupta"; wchar_t wc; int count = 0; for (int i = 0; i < strlen(str); i++) { wc = btowc(str[i]); if (wc != WEOF) count++; } cout << count << " out of " << strlen(str) << " Characters were successfully widened"; return 0; } |
12 out of 13 Characters were successfully widened
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- wcscpy() function in C++ with Examples
- wcscmp() function in C++ with Examples
- quick_exit() function in C++ with Examples
- wcsspn() function in C/C++ with Examples
- wcslen() function in C++ with Examples
- conj() function in C++ with Examples
- norm() function in C++ with Examples
- getdate() and setdate() function in C with Examples
- not1 and not2 function templates in C++ STL with Examples
- ios fail() function in C++ with Examples
- ios bad() function in C++ with Examples
- ios setstate() function in C++ with Examples
- ios good() function in C++ with Examples
- ios eof() function in C++ with Examples
- ios clear() function in C++ with Examples
- ios rdstate() function in C++ with Examples
- ios operator() function in C++ with Examples
- ios operator() function in C++11 with Examples
- ios operator !() function in C++ with Examples
- time.h localtime() function in C with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

