📜  C / C++中的btowc()函数与示例

📅  最后修改于: 2021-05-26 01:27:08             🧑  作者: Mango

btowc()是C / C++中的内置函数,可将字符转换为等效的宽字符。它在C++的cwchar头文件中定义。

语法

wint_t btowc( int ch );

参数:该函数接受一个强制参数ch指定的单字节字符转换其到它的宽字符。

返回值
如果ch是有效的单字节字符,则该函数返回ch的宽字符表示形式。如果ch是EOF ,则返回WEOF

下面的程序说明了上述函数。

程序1

// Program to illustrate
// btowc() function
#include 
#include 
#include 
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

程序2

// Program to illustrate
// btowc() function
#include 
#include 
#include 
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
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”