概述

C++基本数据类型

类型 关键字
布尔型 bool
字符型 char
整型 int
浮点型 float
双浮点型 double
无类型 void
宽字符型 wchar_t

一些基本类型可以使用一个或多个类型修饰符进行修饰:

  • signed
  • unsigned
  • short
  • long

各数据类型在内存中所占字节的大小随系统的差异而变,可通过sizeof()函数查看,下面直接总结一些基本类型所占字节的大小。

所占字节大小总结

类型 32位系统 64位系统
bool 1 1
char 1 1
short 2 2
int 4 4
long 4 4
double 8 8
float 4 4
* 4 8

注:有的地方说64位系统中long占8字节,不过在我的电脑中实测确是4字节,所以还是要根据实际的硬件及编译器确定到底占用多少!

测试程序

几点说明:

  • signed, unsigned, long和short都隐含了int, 等价于signed int, unsigned int, long int, short int
  • 但注意char没有这种默认等价性。char不和signed char或者unsigned char其中任何一个等价。
    如果用于文本,则使用未加限定的char, 是类似于 'a', '0'的类型, 或是组成C字符串"abcde"的类型。它也可以是一个值,但是是当做无符号还是有符号数没有指定。
    如果用于数字,那么: signed char, 范围 -128 到 127,unsigned char, 范围 0 ~ 255。
  • signed与signed int与int是等价类型
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include<iostream>  
#include<string>
#include <limits>
using namespace std;

int main()
{
/*
signed, unsigned, long和short都隐含了int, 等价于signed int, unsigned int, long int, short int
但注意char没有这种默认等价性。char不和signed char或者unsigned char其中任何一个等价。使用char的时候最好标明是signed char还是unsigned char。
signed与signed int与int是等价类型
*/
cout << "type: \t\t" << "************size**************" << endl;
// *
cout << "--------------------------------" << endl;
int *p;
cout << "*p: \t\t" << "所占字节数:" << sizeof(p) << endl;

// bool
cout << "--------------------------------" << endl;
cout << "bool: \t\t" << "所占字节数:" << sizeof(bool);
cout << "\t最大值:" << (numeric_limits<bool>::max)();
cout << "\t\t最小值:" << (numeric_limits<bool>::min)() << endl;

// char
cout << "--------------------------------" << endl;
cout << "char: \t\t" << "所占字节数:" << sizeof(char);
cout << "\t最大值:" << (numeric_limits<char>::max)();
cout << "\t\t最小值:" << (numeric_limits<char>::min)() << endl;

cout << "signed char: \t" << "所占字节数:" << sizeof(signed char);
cout << "\t最大值:" << (numeric_limits<signed char>::max)();
cout << "\t\t最小值:" << (numeric_limits<signed char>::min)() << endl;

cout << "unsigned char: \t" << "所占字节数:" << sizeof(unsigned char);
cout << "\t最大值:" << (numeric_limits<unsigned char>::max)();
cout << "\t\t最小值:" << (numeric_limits<unsigned char>::min)() << endl;

// short (typedef short int wchar_t;)
cout << "--------------------------------" << endl;
cout << "short: \t\t" << "所占字节数:" << sizeof(short);
cout << "\t最大值:" << (numeric_limits<short>::max)();
cout << "\t\t最小值:" << (numeric_limits<short>::min)() << endl;

cout << "unsigned short:\t" << "所占字节数:" << sizeof(unsigned short);
cout << "\t最大值:" << (numeric_limits<unsigned short>::max)();
cout << "\t\t最小值:" << (numeric_limits<unsigned short>::min)() << endl;

cout << "wchar_t: \t" << "所占字节数:" << sizeof(wchar_t);
cout << "\t最大值:" << (numeric_limits<wchar_t>::max)();
cout << "\t\t最小值:" << (numeric_limits<wchar_t>::min)() << endl;

// int
cout << "--------------------------------" << endl;
cout << "int: \t\t" << "所占字节数:" << sizeof(int);
cout << "\t最大值:" << (numeric_limits<int>::max)();
cout << "\t最小值:" << (numeric_limits<int>::min)() << endl;

cout << "unsigned: \t" << "所占字节数:" << sizeof(unsigned);
cout << "\t最大值:" << (numeric_limits<unsigned>::max)();
cout << "\t最小值:" << (numeric_limits<unsigned>::min)() << endl;

// long
cout << "--------------------------------" << endl;
cout << "long: \t\t" << "所占字节数:" << sizeof(long);
cout << "\t最大值:" << (numeric_limits<long>::max)();
cout << "\t最小值:" << (numeric_limits<long>::min)() << endl;

cout << "unsigned long: \t" << "所占字节数:" << sizeof(unsigned long);
cout << "\t最大值:" << (numeric_limits<unsigned long>::max)();
cout << "\t最小值:" << (numeric_limits<unsigned long>::min)() << endl;

// double
cout << "--------------------------------" << endl;
cout << "double: \t" << "所占字节数:" << sizeof(double);
cout << "\t最大值:" << (numeric_limits<double>::max)();
cout << "\t最小值:" << (numeric_limits<double>::min)() << endl;

cout << "long double: \t" << "所占字节数:" << sizeof(long double);
cout << "\t最大值:" << (numeric_limits<long double>::max)();
cout << "\t最小值:" << (numeric_limits<long double>::min)() << endl;

// float
cout << "--------------------------------" << endl;
cout << "float: \t\t" << "所占字节数:" << sizeof(float);
cout << "\t最大值:" << (numeric_limits<float>::max)();
cout << "\t最小值:" << (numeric_limits<float>::min)() << endl;

cout << "size_t: \t" << "所占字节数:" << sizeof(size_t);
cout << "\t最大值:" << (numeric_limits<size_t>::max)();
cout << "\t最小值:" << (numeric_limits<size_t>::min)() << endl;

cout << "string: \t" << "所占字节数:" << sizeof(string) << endl;
// << "\t最大值:" << (numeric_limits<string>::max)() << "\t最小值:" << (numeric_limits<string>::min)() << endl;

cout << "type: \t\t" << "************size**************" << endl;

return 0;
}

64位编译器结果(Visual Studio x64)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
type:           ************size**************
--------------------------------
*p: 所占字节数:8
--------------------------------
bool: 所占字节数:1 最大值:1 最小值:0
--------------------------------
char: 所占字节数:1 最大值: 最小值:€
signed char: 所占字节数:1 最大值: 最小值:€
unsigned char: 所占字节数:1 最大值: 最小值:
--------------------------------
short: 所占字节数:2 最大值:32767 最小值:-32768
unsigned short: 所占字节数:2 最大值:65535 最小值:0
wchar_t: 所占字节数:2 最大值:65535 最小值:0
--------------------------------
int: 所占字节数:4 最大值:2147483647 最小值:-2147483648
unsigned: 所占字节数:4 最大值:4294967295 最小值:0
--------------------------------
long: 所占字节数:4 最大值:2147483647 最小值:-2147483648
unsigned long: 所占字节数:4 最大值:4294967295 最小值:0
--------------------------------
double: 所占字节数:8 最大值:1.79769e+308 最小值:2.22507e-308
long double: 所占字节数:8 最大值:1.79769e+308 最小值:2.22507e-308
--------------------------------
float: 所占字节数:4 最大值:3.40282e+38 最小值:1.17549e-38
size_t: 所占字节数:8 最大值:18446744073709551615 最小值:0
string: 所占字节数:40
type: ************size**************

32位编译器结果(Visual Studio x86)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
type:           ************size**************
--------------------------------
*p: 所占字节数:4
--------------------------------
bool: 所占字节数:1 最大值:1 最小值:0
--------------------------------
char: 所占字节数:1 最大值: 最小值:€
signed char: 所占字节数:1 最大值: 最小值:€
unsigned char: 所占字节数:1 最大值: 最小值:
--------------------------------
short: 所占字节数:2 最大值:32767 最小值:-32768
unsigned short: 所占字节数:2 最大值:65535 最小值:0
wchar_t: 所占字节数:2 最大值:65535 最小值:0
--------------------------------
int: 所占字节数:4 最大值:2147483647 最小值:-2147483648
unsigned: 所占字节数:4 最大值:4294967295 最小值:0
--------------------------------
long: 所占字节数:4 最大值:2147483647 最小值:-2147483648
unsigned long: 所占字节数:4 最大值:4294967295 最小值:0
--------------------------------
double: 所占字节数:8 最大值:1.79769e+308 最小值:2.22507e-308
long double: 所占字节数:8 最大值:1.79769e+308 最小值:2.22507e-308
--------------------------------
float: 所占字节数:4 最大值:3.40282e+38 最小值:1.17549e-38
size_t: 所占字节数:4 最大值:4294967295 最小值:0
string: 所占字节数:28
type: ************size**************

参考:菜鸟教程C++数据类型

附加

数组,结构体等的占用大小测试,注释后面的数字是sizeof后的大小,64位与32位只有指针那个有差别。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
char a1[] = "abc";//4->字符数组末尾自动添加 '\0'
char a2[] = { 'a','b','c' };//3
char* a3 = "abc";//8(4)
char b[20] = "abc";//20->内存中预分配的大小为20×1
//结构体,字节对齐->结构体的大小等于结构体内最大成员大小的整数倍
struct {
double i2;
int i1;
char c;
}S1;//16
struct {
int i1;
double i2;
char c;
}S2;//24
//结构体+联合体(内存共享)
struct {
int i1;
char c1;
union
{
int i2;
int i3;
char c2;
};
}S3;//12