btowc() function in C/C++ with Examples

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Output:

14 out of 15 Characters were successfully widened

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Output:

12 out of 13 Characters were successfully widened


My Personal Notes arrow_drop_up

Budding Web DeveloperKeen learnerAverage CoderDancer&Social Activist

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.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.