本节我们介绍几个c++关键字:
- wchar_t :宽字节
- bool :布尔类型
- longlong :长长整形(仅在 64 位平台上是有效声明类型)
- auto :自动类型
老规矩,看代码:
#include <iostream>
using namespace std;void test01_wchar();
void test02_bool();
void test03_longlong();
void test04_auto();int main()
{// wchar_类型测试test01_wchar();// bool类型测试test02_bool();// longlong类型测试test03_longlong();// auto类型测试test04_auto();return 0;
}/// @brief wchar_t类型测试
void test01_wchar()
{cout << "wchar_t类型测试:" << endl;//单字节字符,1个字节char a = 'A';//宽字节字符,两个字节wchar_t b = L'B';wchar_t c = L'中';cout << "单字节a为:" << a << ",\t大小为:" << sizeof(a) << endl;//宽字符输出用wcoutwcout << "宽字节b为:" << b << ",\t大小为:" << sizeof(b) << endl;//默认Locale是EN_US, 需设置为chscout << "中文c为:";wcout.imbue(locale("chs"));wcout << c;cout.imbue(locale("en_us"));cout << ",\t大小为:" << sizeof(c) << endl << endl;
}/// @brief bool类型测试
void test02_bool()
{cout << "bool类型测试:" << endl;//布尔值只有两种0 1bool b1 = true;bool b2 = false;bool b3 = 0;//0为假bool b4 = -1;//非0为真//布尔类型占1个字节cout << "bool类型大小: " << sizeof(b1) << endl;cout << "bool b1大小:" << b1 << endl;cout << "bool b2大小:" << b2 << endl;cout << "bool b3大小:" << b3 << endl;cout << "bool b4大小:" << b4 << endl << endl;
}/// @brief longlong类型测试
void test03_longlong()
{cout << "longlong类型测试:" << endl;//扩展8字节的数据long c1 = 4294967296;//2的32次方cout << "long c1大小为:" << sizeof(c1) << ",\t内容为 " << c1 << endl;long long c2 = 4294967296;//2的 64次方cout << "longlong c2大小为:" << sizeof(c2) << ",\t内容为 " << c2 << endl << endl;}/// @brief auto类型测试
void test04_auto()
{cout << "auto类型测试:" << endl;//auto类型auto a1 = 99;auto a2 = 'B';auto a3 = L'C';auto a4 = 1.23f;auto a5 = &a1;//auto a6;//需要初始值int e[2][3];auto a6 = e;cout << "typeid(a1).name:" << typeid(a1).name() << endl;cout << "typeid(a2).name:" << typeid(a2).name() << endl;cout << "typeid(a3).name:" << typeid(a3).name() << endl;cout << "typeid(a4).name:" << typeid(a4).name() << endl;cout << "typeid(a5).name:" << typeid(a5).name() << endl;cout << "typeid(a6).name:" << typeid(a6).name() << endl << endl;
}
效果如下: