Open In App
Related Articles

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

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

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 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

CPP

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

CPP

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


Last Updated : 13 Feb, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads