Open In App

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

Last Updated : 13 Feb, 2023
Comments
Improve
Suggest changes
Like Article
Like
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

Next Article
Article Tags :
Practice Tags :

Similar Reads