1. 整型
1.1 整型
char、short、int、long、long long
默认为有符号的,例如 signed int;
- 规则:
- short:至少16位;
- int:至少和short一样;
- long:至少32位,且至少和int一样长;
- long long:至少64位,且至少和long一样长。
除了char其余都有无符号变体:
unsigned short、unsigned int、unsigned long、unsigned long long
注意
unsigned 本身是 unsigned int 的缩写。
计算机位数 | char | char16_t | char32_t | short | int | long | long long | pointer |
---|---|---|---|---|---|---|---|---|
32位 | 1 | 2 | 4 | 2 | 2 | 4 | 8 | 4 |
64位 | 1 | 2 | 4 | 2 | 4 | 4 | 8 | 8 |
可以看到,在32位和64位计算机上,对于整形唯一不同的就是int和指针的大小,其中32位计算机上int占2字节,64位上占4字节。指针大小和int大小相同。
#include <iostream>
using namespace std;
int main()
{
//测试机 64位
// 32位信息来源于互联网,应该没错
cout <<"size of char is: "<< sizeof(char) << " bytes" << endl;
cout <<"size of char16_t is: "<< sizeof(char16_t) << " bytes" << endl;
cout <<"size of char32_t is: "<< sizeof(char32_t) << " bytes" << endl;
cout <<"size of short is: "<< sizeof(short) << " bytes" << endl;
cout <<"size of int is: "<< sizeof(int) << " bytes" << endl;
cout <<"size of long is: "<< sizeof(long) << " bytes" << endl;
int *p;
cout <<"size of pointer is: "<< sizeof(p) << " bytes" << endl;
cout <<"size of long long is: "<< sizeof(long long) << " bytes" << endl;
return 1;
}
/*
size of char is: 1 bytes
size of char16_t is: 2 bytes
size of char32_t is: 4 bytes
size of short is: 2 bytes
size of int is: 4 bytes
size of long is: 4 bytes
size of pointer is: 8 bytes
size of long long is: 8 bytes
*/
1.2 wchar_t
char是8位字符类型,最多只能包含256种字符,许多外文字符集所含的字符数目超过256个,char型无法表示。
wchar_t数据类型一般为16位或32位,但不同的C或C++库有不同的规定,如GNU Libc规定wchar_t为32位,总之,wchar_t所能表示的字符数远超char型。
标准C++中的wprintf()函数以及iostream类库中的类和对象能提供wchar_t宽字符类型的相关操作。
#include <iostream>
using namespace std;
int main()
{
//测试机 64位
cout <<"size of char is: "<< sizeof(char) << " bytes" << endl;
cout <<"size of wchar_t is: "<< sizeof(wchar_t) << " bytes" << endl;
return 1;
}
/*
size of char is: 1 bytes
size of wchar_t is: 2 bytes
*/
1.3 C ++11新增类型:char16_t和char32_t
- char16_t:16位无符号->前缀u表示
- char32_t:32位无符号->前缀U表示
值得注意的是VS2013还没有支持char16_t和char32_t
float | double | long double | |
---|---|---|---|
32位 | |||
64位 | 4 | 8 | 16 |
#include <iostream>
using namespace std;
int main()
{
//测试机 64位
cout <<"size of foat is: "<< sizeof(float) << " bytes" << endl;
cout <<"size of double is: "<< sizeof(double) << " bytes" << endl;
cout <<"size of long double is: "<< sizeof(long double) << " bytes" << endl;
return 1;
}
/*
size of foat is: 4 bytes
size of double is: 8 bytes
size of long double is: 16 bytes
*/
2. 浮点型
3个浮点型:float、double、long double
规则:
- float至少4字节;
- double至少6字节,且不少于float;
- long double至少和double一样多。
3. 前缀和后缀
3.1. 前缀:
0开头表示8进制,0x或0X开头表示16进制。
示例:
#include <iostream>
using namespace std;
int main()
{
//测试机 64位
int test8 = 071;
cout << "071 is " << test8 << endl;
int test16 = 0xE5;
cout << "0xE5 is " << test16;
return 1;
}
/*
071 is 57
0xE5 is 229
*/
3.2 后缀:
- l或L表示long常量
- u或U表示unsigned int常量
- ul、uL、Ul、UL、lU、lu、LU、Lu表示unsigned long常量。(lu可以采用任意顺序和大小写表示)
- ll、LL表示long long 常量。
- ull、Ull、uLL、ULL表示unsigned long long常量。
3.3 对于浮点常量,
- f或F表示float
- l或L表示long double
- 其他都表示double
3.4 不带后缀时的规则——尽可能采用小的类型:
- 对于10进制:int->long->long long
- 对于8进制或16进制:int->unsigned int->long->unsigned long->long long ->unsigned long long
参考: 《C++ Primer Plus》、不只是程序员的博客