蚊子132网,上篇内容讲述了整个语言的发展【为什么会产生编程语言】,以及学习C++所需要掌握的内容。
这节开始认识第一部分最基本的内容:C++的内建类型,也就是基本类型。
在这些知识之前留一个问题:为什么基本所有语言中都有相似的这些基本类型。
bool : 用于表示真/假
bool b_ret1 = true;bool b_ret2 = false;
short /long :short用于缩减/增大整数表示的范围,也就是占用内存的多少。
signed / unsigned :指定符号。但不会改变内存大小。
int(signed int) / unsigned int :表示一般意义上的整数,两者的区别在于有无符号。
加上short,signed,unsigned后,就可以控制整数的范围。所以有了多种整数:
int(signed int) / unsigned int
signed short int / unsigned short int
signed long int / unsigned long int
int age = 20;unsigned int age2 = 20;short int age3 = 20;unsigned short int small_int = 22222;unsigned long int big_number = 2222222222;
char(signed char) / unsigned char :表示字符。编程中用char已经足够了,效率也不会用影响。
这个类型有些纠结,本意是表示字符的,但是经常用它的数组存储数据,等说到指针的时候,再详细说吧。
char letter_char = 'c';
wchar_t :宽字节字符。由于char在计算机存储中占用一个字节,最多能表示256个字符,
英语字符是足够储存,但是光汉字就有上万个,char已经无能为力了。
所以wchar_t占两个字节,甚至四个字节,完成足够表示其它民族的语言。
wchar_t wide_char = L'c'
float: 单精度浮点 double:双精度浮点 long double :扩展精度
两个类型在概念上都是小数,double比float多占用了内存空间,表示的更精确。一般的小数用float就可以了。
float _length = 3.5f;double _length = 3.5555long double l_legnth = 3.5e-3L
void:空类型。一般用表示无返回值 ,或者不确定类型的对象。
return void;void* p = 0;
Windows XP 32位平台下的取值范围:
Type | Size | 数值范围 |
无值型void | 0 byte | 无值域 |
布尔型bool | 1 byte | true false |
有符号短整型short [int] /signed short [int] | 2 byte | -32768~32767 |
无符号短整型unsigned short [int] | 2 byte | 0~65535 |
有符号整型int /signed [int] | 4 byte | -2147483648~2147483647 |
无符号整型unsigned [int] | 4 byte | 0~4294967295 |
有符号长整型long [int]/signed long [int] | 4 byte | -2147483648~2147483647 |
无符号长整型unsigned long [int] | 4 byte | 0~4294967295 |
long long | 8 byte | 0~18446744073709552000 |
有符号字符型char/signed char | 1 byte | -128~127 |
无符号字符型unsigned char | 1 byte | 0~255 |
宽字符型wchar_t (unsigned short.) | 2 byte | 0~65535 |
单精度浮点型float | 4 byte | -3.4E-38~3.4E+38 |
双精度浮点型double | 8 byte | 1.7E-308~1.7E+308 |
long double | 8 byte |
由于平台和编译器的不同,每种类型的范围及所占内存大小并不确定。想具体确定可以用下边代码