4ac++ primer宽字符wchar_t显示设置与输出代码示例

..

#include <iostream>
#include <windows.h>
#include <locale>
//#include<wchar.h>
//#include <stdlib.h>
int main()
{
    short sh;
    unsigned short sh1;
    sh = 100000;
    sh1 = 100000;
    std::cout << "short类型的sh的值是: " << sh << std::endl;
    std::cout << "unsigned short类型的sh1的值是: " << sh1 << std::endl;
    //100000大于65535的存储范围,溢出了。所以100000%65536=34464
    std::cout << "ungined没有short的size是: " << sizeof(signed) << "" << sizeof(signed) * 8 << "" << std::endl;
    std::cout << "int的size是: " << sizeof(int) << "" << sizeof(int) * 8 << "" << std::endl;
    std::cout << "char的size是: " << sizeof(char) << "" << sizeof(char) * 8 << "" << std::endl;
    std::cout << "wchar_t的size是: " << sizeof(wchar_t) << "" << sizeof(wchar_t) * 8 << "" << std::endl;
    std::cout << "bool的size是: " << sizeof(bool) << "" << sizeof(bool) * 8 << "" << std::endl;
    std::cout << "float的size是: " << sizeof(float) << "" << sizeof(float) * 8 << "" << std::endl;
    std::cout << "double的size是: " << sizeof(double) << "" << sizeof(double) * 8 << "" << std::endl;
    std::cout << "long的size是: " << sizeof(long) << "" << sizeof(long) * 8 << "" << std::endl;
    printf("test it\n");
    printf("测试\n");
    char a[] = "大家好吗";
    printf("%s\n",a);
    wchar_t bb1= L'';
    wchar_t bb2[] = L"需要多测试";
    //locale loc("chs");//windows下ok
    //setlocale(LC_ALL, "chs");
    //std::wcout.imbue(std::loc);
    //std::wcout << bb1 << std::endl;
    printf("%c\n",'x');
    setlocale(LC_ALL, "chs");//需要设置语言,宽字符才会显示
    wprintf(L"%lc\n",L'');
    wprintf(L"%lc\n", bb1);
    wprintf(L"%ls\n", bb2);
    wprintf(L"%lc\n", bb2[4]);
    wprintf(L"%lc\n", bb2[0]);
    //printf("%lc",bb);
    //Sleep(500000);
    //short 16位   有一个符号位:所以是-2的15次方(-32768)~+32767。 
    //unsigned short 16位 无符号0-65535
    //unsigned 32位
    //int 32位
    //char 8位
    //wchar_t 16位
    //bool 8位
    //float 32
    //double 64
    getchar();
    return 0;
}
欢迎讨论,相互学习。 txwtech@163.com
原文地址:https://www.cnblogs.com/txwtech/p/11785912.html