Skip to content
Related Articles

Related Articles

btowc() function in C/C++ with Examples
  • Last Updated : 04 Sep, 2018

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;
}
Output:
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;
}
Output:
12 out of 13 Characters were successfully widened
Want to learn from the best curated videos and practice problems, check out the C++ Foundation Course for Basic to Advanced C++ and C++ STL Course for foundation plus STL.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.
My Personal Notes arrow_drop_up
Recommended Articles
Page :