学而不思则罔,思而不学则殆
【C++】C++基本数据类型
写在前面,为啥做为一个Android开发要学习C++?
本人在大学学了一学期C++过后,后来就再也没有接触过C++,工作四年多也一直使用的是Java.C++知识早就全部忘光了。那这里又来重新学习是为啥呢?还不是被逼的,没办法啊,Android中的IPC核心部分在Native代码中,JNI中,都是C++实现的。Linux内核也是C和C++代码。不会C++,没办法啊。逼着自己学习
所以我也开始了苦逼的学习C++,为了不让我学完就忘,特把我的学习过程记录总结下来。便于后面复习,也方便大家监督
今天来学C++的基本数据类型
一.C++数据类型
类型 | 关键字 | 大小(字节) |
---|---|---|
布尔型 | bool | 1(8位) |
字符型 | char | 1(8位) |
整型 | int | 4(32位) |
浮点型 | float | 4(32位) |
双浮点型 | double | 8(64位) |
无类型 | void | - |
二.C++数据类型修饰符
修饰符 | 说明 |
---|---|
signed | 有符号的 |
unsigned | 无符号的 |
short | 短型 |
long | 长型 |
三.基本数据类型+修饰符
3.1 总结
类型 | 说明 | 字节数 |
---|---|---|
bool | 布尔型 | 1 |
char | 字符型 | 1 |
unsigned char | 无符号字符 | 1 |
signed char | 有符号字符 | 1 |
int | 整型 | 4 |
unsigned int | 无符号整型 | 4 |
signed int | 有符号整型 | 4 |
short int | 短整型 | 2 |
unsigned short int | 无符号短整型 | 2 |
signed short int | 有符号短整型 | 2 |
long int | 长整型 | 4 |
unsigned long int | 无符号长整型 | 4 |
signed long int | 有符号长整型 | 4 |
float | 单精度浮点型 | 4 |
double | 双精度浮点型 | 8 |
long double | 长双单精度浮点型 | 8 |
从上表可得知,变量的大小会根据编译器和所使用的电脑而有所不同。
3.2 测试
3.2.1 bool
cout << "bool\t" << "所占字节数:" << sizeof(bool) << "\t最大值:\t" << (numeric_limits<bool>::max)()<< "\t最小值:\t"<< (numeric_limits<bool>::min)() <<endl;
//bool 所占字节数:1 最大值: 1 最小值: 0
3.2.2 char
cout << "char\t" << "所占字节数:" << sizeof(char) << "\t最大值:\t" << (numeric_limits<char>::max)()<< "\t最小值:\t"<< (numeric_limits<char>::min)() <<endl;
cout << "unsigned char\t" << "所占字节数:" << sizeof(unsigned char) << "\t最大值:\t" << (numeric_limits<unsigned char>::max)()<< "\t最小值:\t"<< (numeric_limits<unsigned char>::min)() <<endl;
cout << "signed char\t" << "所占字节数:" << sizeof(signed char) << "\t最大值:\t" << (numeric_limits<signed char>::max)()<< "\t最小值:\t"<< (numeric_limits<signed char>::min)() <<endl;
char 所占字节数:1 最大值: 最小值: €
unsigned char 所占字节数:1 最大值: 最小值:
signed char 所占字节数:1 最大值: 最小值: €
3.2.3 int
cout << "int\t" << "所占字节数:" << sizeof(int) << "\t最大值:\t" << (numeric_limits<int>::max)()<< "\t最小值:\t"<< (numeric_limits<int>::min)() <<endl;
cout << "unsigned int\t" << "所占字节数:" << sizeof(unsigned int) << "\t最大值:\t" << (numeric_limits<unsigned int>::max)()<< "\t最小值:\t"<< (numeric_limits<unsigned int>::min)() <<endl;
cout << "signed int\t" << "所占字节数:" << sizeof(signed int) << "\t最大值:\t" << (numeric_limits<signed int>::max)()<< "\t最小值:\t"<< (numeric_limits<signed int>::min)() <<endl;
int 所占字节数:4 最大值: 2147483647 最小值: -2147483648
unsigned int 所占字节数:4 最大值: 4294967295 最小值: 0
signed int 所占字节数:4 最大值: 2147483647 最小值: -2147483648
3.2.4 short int
cout << "short int\t" << "所占字节数:" << sizeof(short int) << "\t最大值:\t" << (numeric_limits<short int>::max)()<< "\t最小值:\t"<< (numeric_limits<short int>::min)() <<endl;
cout << "unsigned short int\t" << "所占字节数:" << sizeof(unsigned short int) << "\t最大值:\t" << (numeric_limits<unsigned short int>::max)()<< "\t最小值:\t"<< (numeric_limits<unsigned short int>::min)() <<endl;
cout << "signed short int\t" << "所占字节数:" << sizeof(signed short int) << "\t最大值:\t" << (numeric_limits<signed short int>::max)()<< "\t最小值:\t"<< (numeric_limits<signed short int>::min)() <<endl;
short int 所占字节数:2 最大值: 32767 最小值: -32768
unsigned short int 所占字节数:2 最大值: 65535 最小值: 0
signed short int 所占字节数:2 最大值: 32767 最小值: -32768
3.2.5 long int
cout << "long int\t" << "所占字节数:" << sizeof(long int) << "\t最大值:\t" << (numeric_limits<long int>::max)()<< "\t最小值:\t"<< (numeric_limits<long int>::min)() <<endl;
cout << "signed long int\t" << "所占字节数:" << sizeof(signed long int) << "\t最大值:\t" << (numeric_limits<signed long int>::max)()<< "\t最小值:\t"<< (numeric_limits<signed long int>::min)() <<endl;
cout << "unsigned long int\t" << "所占字节数:" << sizeof(unsigned long int) << "\t最大值:\t" << (numeric_limits<unsigned long int>::max)()<< "\t最小值:\t"<< (numeric_limits<unsigned long int>::min)() <<endl;
long int 所占字节数:4 最大值: 2147483647 最小值: -2147483648
signed long int 所占字节数:4 最大值: 2147483647 最小值: -2147483648
unsigned long int 所占字节数:4 最大值: 4294967295 最小值: 0
3.2.6 float
cout << "float\t" << "所占字节数:" << sizeof(float) << "\t最大值:\t" << (numeric_limits<float>::max)()<< "\t最小值:\t"<< (numeric_limits<float>::min)() <<endl;
float 所占字节数:4 最大值: 3.40282e+38 最小值: 1.17549e-38
3.2.7 double
cout << "double\t" << "所占字节数:" << sizeof(double) << "\t最大值:\t" << (numeric_limits<double>::max)()<< "\t最小值:\t"<< (numeric_limits<double>::min)() <<endl;
cout << "long double\t" << "所占字节数:" << sizeof(long double) << "\t最大值:\t" << (numeric_limits<long double>::max)()<< "\t最小值:\t"<< (numeric_limits<long double>::min)() <<endl;
double 所占字节数:8 最大值: 1.79769e+308 最小值: 2.22507e-308
long double 所占字节数:8 最大值: 1.79769e+308 最小值: 2.22507e-308
整体总结
四.typedef 声明
您可以使用 typedef 为一个已有的类型取一个新的名字。下面是使用 typedef 定义一个新类型的语法:
typedef type newname;
typedef int Integer;
Integer x = 90;
cout << "feet:" << x << endl;
typedef char Char;
Char ch = 'a';
cout << "ch:" << ch << endl;
定义一个int的别名feet,和char别名Char.
测试结果:
feet:90
ch:a
五.枚举类型
枚举类型(enumeration)是C++中的一种派生数据类型,它是由用户定义的若干枚举常量的集合。
创建枚举,需要使用关键字 enum。枚举类型的一般形式为:
enum 枚举名{
标识符[=整型常数],
标识符[=整型常数],
...
标识符[=整型常数]
} 枚举变量;
默认情况下,第一个名称的值为 0,第二个名称的值为 1,第三个名称的值为 2,以此类推。但是,您也可以给名称赋予一个特殊的值,只需要添加一个初始值即可。例如,在下面的枚举中,STOPED的值为 5。
enum ActivityStatus { CREATED, STARTED, RESUMED,PAUSED,STOPED=9,DESTORYED } statu;
statu = DESTORYED;
cout << "statu:" << statu << "\t" << sizeof(statu) << endl;
//statu:10 4