C++ 数据类型
使用编程语言进行编程时,需要用到各种变量来存储各种信息。变量保留的是它所存储的值的内存位置。这意味着,当您创建一个变量时,就会在内存中保留一些空间。
您可能需要存储各种数据类型(比如字符型、宽字符型、整型、浮点型、双浮点型、布尔型等)的信息,操作系统会根据变量的数据类型,来分配内存和决定在保留内存中存储什么。
基本的内置类型
C++ 为程序员提供了种类丰富的内置数据类型和用户自定义的数据类型。下表列出了七种基本的 C++ 数据类型:
| 布尔型 | bool |
| 字符型 | char |
| 整型 | int |
| 浮点型 | float |
| 双浮点型 | double |
| 无类型 | void |
| 宽字符型 |
wchar_t |
其实 wchar_t 是这样来的:
typedef short int wchar_t;
所以 wchar_t 实际上的空间是和 short int 一样。
一些基本类型可以使用一个或多个类型修饰符进行修饰:
| signed | 表示有符号类型(默认) | signed int x = -10; |
| unsigned | 表示无符号类型 | unsigned int y = 10; |
| short | 表示短整型 | short int z = 100; |
| long | 表示长整型 | long int a = 100000; |
| const | 表示常量,值不可修改 | const int b = 5; |
| volatile | 表示变量可能被意外修改,禁止编译器优化 | volatile int c = 10; |
| mutable | 表示类成员可以在 const 对象中修改 | mutable int counter; |
下表显示了各种变量类型在内存中存储值时需要占用的内存,以及该类型的变量所能存储的最大值和最小值。
注意:不同系统会有所差异,一字节为 8 位。
注意:默认情况下,int、short、long都是带符号的,即 signed。
注意:long int 8 个字节,int 都是 4 个字节,早期的 C 编译器定义了 long int 占用 4 个字节,int 占用 2 个字节,新版的 C/C++ 标准兼容了早期的这一设定。
| bool | 布尔类型,表示真或假 | 1 | true 或 false |
| char | 字符类型,通常用于存储 ASCII 字符 | 1 | -128 到 127 或 0 到 255 |
| signed char | 有符号字符类型 | 1 | -128 到 127 |
| unsigned char | 无符号字符类型 | 1 | 0 到 255 |
| wchar_t | 宽字符类型,用于存储 Unicode 字符 | 2 或 4 | 取决于平台 |
| char16_t | 16 位 Unicode 字符类型(C++11 引入) | 2 | 0 到 65,535 |
| char32_t | 32 位 Unicode 字符类型(C++11 引入) | 4 | 0 到 4,294,967,295 |
| short | 短整型 | 2 | -32,768 到 32,767 |
| unsigned short | 无符号短整型 | 2 | 0 到 65,535 |
| int | 整型 | 4 | -2,147,483,648 到 2,147,483,647 |
| unsigned int | 无符号整型 | 4 | 0 到 4,294,967,295 |
| long | 长整型 | 4 或 8 | 取决于平台 |
| unsigned long | 无符号长整型 | 4 或 8 | 取决于平台 |
| long long | 长长整型(C++11 引入) | 8 | -9,223,372,036,854,775,808 到 9,223,372,036,854,775,807 |
| unsigned long long | 无符号长长整型(C++11 引入) | 8 | 0 到 18,446,744,073,709,551,615 |
| float | 单精度浮点数 | 4 | 约 ±3.4e±38(6-7 位有效数字) |
| double | 双精度浮点数 | 8 | 约 ±1.7e±308(15 位有效数字) |
| long double | 扩展精度浮点数 | 8、12 或 16 | 取决于平台 |
C++11 新增类型
| auto | 自动类型推断 | auto x = 10; |
| decltype | 获取表达式的类型 | decltype(x) y = 20; |
| nullptr | 空指针常量 | int* ptr = nullptr; |
| std::initializer_list | 初始化列表类型 | std::initializer_list<int> list = {1, 2, 3}; |
| std::tuple | 元组类型,可以存储多个不同类型的值 | std::tuple<int, float, char> t(1, 2.0, 'a'); |
注意,各种类型的存储大小与系统位数有关,但目前通用的以64位系统为主。
以下列出了32位系统与64位系统的存储大小的差别(windows 相同):

从上表可得知,变量的大小会根据编译器和所使用的电脑而有所不同。
下面实例会输出您电脑上各种数据类型的大小。
https://avg.163.com/topic/detail/11122377
https://avg.163.com/topic/detail/11122376
https://avg.163.com/topic/detail/11122374
https://avg.163.com/topic/detail/11122375
https://avg.163.com/topic/detail/11122373
https://avg.163.com/topic/detail/11122372
https://avg.163.com/topic/detail/11122371
https://avg.163.com/topic/detail/11122345
https://avg.163.com/topic/detail/11122370
https://avg.163.com/topic/detail/11122369
https://avg.163.com/topic/detail/11122368
https://avg.163.com/topic/detail/11122367
https://avg.163.com/topic/detail/11122366
https://avg.163.com/topic/detail/11122365
https://avg.163.com/topic/detail/11122364
https://avg.163.com/topic/detail/11122363
https://avg.163.com/topic/detail/11122362
https://avg.163.com/topic/detail/11122360
https://avg.163.com/topic/detail/11122359
https://avg.163.com/topic/detail/11122358
https://avg.163.com/topic/detail/11122357
https://avg.163.com/topic/detail/11122356
https://avg.163.com/topic/detail/11122355
https://avg.163.com/topic/detail/11122354
https://avg.163.com/topic/detail/11122353
https://avg.163.com/topic/detail/11122352
https://avg.163.com/topic/detail/11122351
https://avg.163.com/topic/detail/11122350
https://avg.163.com/topic/detail/11122348
https://avg.163.com/topic/detail/11122349
https://avg.163.com/topic/detail/11122347
https://avg.163.com/topic/detail/11122344
https://avg.163.com/topic/detail/11122343
https://avg.163.com/topic/detail/11122342
https://avg.163.com/topic/detail/11122341
https://avg.163.com/topic/detail/11122339
https://avg.163.com/topic/detail/11122340
https://avg.163.com/topic/detail/11122338
https://avg.163.com/topic/detail/11122337
https://avg.163.com/topic/detail/11122336
https://avg.163.com/topic/detail/11122335
https://avg.163.com/topic/detail/11122334
https://avg.163.com/topic/detail/11122333
https://avg.163.com/topic/detail/11122331
https://avg.163.com/topic/detail/11122330
https://avg.163.com/topic/detail/11122329
https://avg.163.com/topic/detail/11122328
https://avg.163.com/topic/detail/11122325
https://avg.163.com/topic/detail/11122327
https://avg.163.com/topic/detail/11122326
https://avg.163.com/topic/detail/11122323
https://avg.163.com/topic/detail/11122324
https://avg.163.com/topic/detail/11122322
https://avg.163.com/topic/detail/11122320
https://avg.163.com/topic/detail/11122321
https://avg.163.com/topic/detail/11122319
https://avg.163.com/topic/detail/11122318
https://avg.163.com/topic/detail/11122317
https://avg.163.com/topic/detail/11122316
https://avg.163.com/topic/detail/11122315
https://avg.163.com/topic/detail/11122314
https://avg.163.com/topic/detail/11122313
https://avg.163.com/topic/detail/11122312
https://avg.163.com/topic/detail/11122311
https://avg.163.com/topic/detail/11122310
https://avg.163.com/topic/detail/11122304
https://avg.163.com/topic/detail/11122309
https://avg.163.com/topic/detail/11122308
https://avg.163.com/topic/detail/11122301
https://avg.163.com/topic/detail/11122307
https://avg.163.com/topic/detail/11122306
https://avg.163.com/topic/detail/11122302
https://avg.163.com/topic/detail/11122305
https://avg.163.com/topic/detail/11122303
https://avg.163.com/topic/detail/11122300
https://avg.163.com/topic/detail/11122299
https://avg.163.com/topic/detail/11122298
https://avg.163.com/topic/detail/11122296
https://avg.163.com/topic/detail/11122297
https://avg.163.com/topic/detail/11122293
https://avg.163.com/topic/detail/11122295
https://avg.163.com/topic/detail/11122294
https://avg.163.com/topic/detail/11122292
https://avg.163.com/topic/detail/11122291
https://avg.163.com/topic/detail/11122289
https://avg.163.com/topic/detail/11122290
https://avg.163.com/topic/detail/11122288
https://avg.163.com/topic/detail/11122287
https://avg.163.com/topic/detail/11122286
https://avg.163.com/topic/detail/11122285
https://avg.163.com/topic/detail/11122284
https://avg.163.com/topic/detail/11122283
https://avg.163.com/topic/detail/11122282
https://avg.163.com/topic/detail/11122281
https://avg.163.com/topic/detail/11122280
https://avg.163.com/topic/detail/11122275
https://avg.163.com/topic/detail/11122277
https://avg.163.com/topic/detail/11122279
https://avg.163.com/topic/detail/11122278
https://avg.163.com/topic/detail/11122274
https://avg.163.com/topic/detail/11122276
https://avg.163.com/topic/detail/11122273
https://avg.163.com/topic/detail/11122272
https://avg.163.com/topic/detail/11122271
https://avg.163.com/topic/detail/11122270
https://avg.163.com/topic/detail/11122269
https://avg.163.com/topic/detail/11122268
https://avg.163.com/topic/detail/11122267
https://avg.163.com/topic/detail/11122266
https://avg.163.com/topic/detail/11122265
https://avg.163.com/topic/detail/11122261
https://avg.163.com/topic/detail/11122264
https://avg.163.com/topic/detail/11122262
https://avg.163.com/topic/detail/11122263
https://avg.163.com/topic/detail/11122260
https://avg.163.com/topic/detail/11122259
https://avg.163.com/topic/detail/11122258
https://avg.163.com/topic/detail/11122257
https://avg.163.com/topic/detail/11122256
https://avg.163.com/topic/detail/11122255
https://avg.163.com/topic/detail/11122254
https://avg.163.com/topic/detail/11122253
https://avg.163.com/topic/detail/11122252
https://avg.163.com/topic/detail/11122251
https://avg.163.com/topic/detail/11122250
https://avg.163.com/topic/detail/11122249
https://avg.163.com/topic/detail/11122238
https://avg.163.com/topic/detail/11122248
https://avg.163.com/topic/detail/11122247
https://avg.163.com/topic/detail/11122240
https://avg.163.com/topic/detail/11122244
https://avg.163.com/topic/detail/11122246
https://avg.163.com/topic/detail/11122245
https://avg.163.com/topic/detail/11122243
https://avg.163.com/topic/detail/11122242
https://avg.163.com/topic/detail/11122241
https://avg.163.com/topic/detail/11122239
https://avg.163.com/topic/detail/11122237
https://avg.163.com/topic/detail/11122236
https://avg.163.com/topic/detail/11122235
https://avg.163.com/topic/detail/11122234
https://avg.163.com/topic/detail/11122233
https://avg.163.com/topic/detail/11122232
https://avg.163.com/topic/detail/11122231
https://avg.163.com/topic/detail/11122230
https://avg.163.com/topic/detail/11122227
https://avg.163.com/topic/detail/11122228
https://avg.163.com/topic/detail/11122226
https://avg.163.com/topic/detail/11122225
https://avg.163.com/topic/detail/11122224
https://avg.163.com/topic/detail/11122223
https://avg.163.com/topic/detail/11122222
https://avg.163.com/topic/detail/11122220
https://avg.163.com/topic/detail/11122219
https://avg.163.com/topic/detail/11122218
https://avg.163.com/topic/detail/11122217
https://avg.163.com/topic/detail/11122216
https://avg.163.com/topic/detail/11122215
https://avg.163.com/topic/detail/11122214
https://avg.163.com/topic/detail/11122211
https://avg.163.com/topic/detail/11122212
https://avg.163.com/topic/detail/11122210
https://avg.163.com/topic/detail/11122209
https://avg.163.com/topic/detail/11122208
https://avg.163.com/topic/detail/11122207
https://avg.163.com/topic/detail/11122206
https://avg.163.com/topic/detail/11122205
https://avg.163.com/topic/detail/11122203
https://avg.163.com/topic/detail/11122204
https://avg.163.com/topic/detail/11122202
https://avg.163.com/topic/detail/11122201
https://avg.163.com/topic/detail/11122200
https://avg.163.com/topic/detail/11122195
https://avg.163.com/topic/detail/11122199
https://avg.163.com/topic/detail/11122198
https://avg.163.com/topic/detail/11122197
https://avg.163.com/topic/detail/11122196
https://avg.163.com/topic/detail/11122191
https://avg.163.com/topic/detail/11122194
https://avg.163.com/topic/detail/11122193
https://avg.163.com/topic/detail/11122192
https://avg.163.com/topic/detail/11122190
https://avg.163.com/topic/detail/11122189
https://avg.163.com/topic/detail/11122187
https://avg.163.com/topic/detail/11122188
https://avg.163.com/topic/detail/11122186
https://avg.163.com/topic/detail/11122185
https://avg.163.com/topic/detail/11122184
https://avg.163.com/topic/detail/11122183
https://avg.163.com/topic/detail/11122182
https://avg.163.com/topic/detail/11122181
https://avg.163.com/topic/detail/11122180
https://avg.163.com/topic/detail/11122179
https://avg.163.com/topic/detail/11122176
https://avg.163.com/topic/detail/11122178
https://avg.163.com/topic/detail/11122177
https://avg.163.com/topic/detail/11122175
https://avg.163.com/topic/detail/11122170
https://avg.163.com/topic/detail/11122174
https://avg.163.com/topic/detail/11122173
https://avg.163.com/topic/detail/11122172
https://avg.163.com/topic/detail/11122171
https://avg.163.com/topic/detail/11122169
https://avg.163.com/topic/detail/11122167
https://avg.163.com/topic/detail/11122168
https://avg.163.com/topic/detail/11122166
https://avg.163.com/topic/detail/11122165
https://avg.163.com/topic/detail/11122164
https://avg.163.com/topic/detail/11122162
https://avg.163.com/topic/detail/11122163
https://avg.163.com/topic/detail/11122161
https://avg.163.com/topic/detail/11122160
https://avg.163.com/topic/detail/11122159
https://avg.163.com/topic/detail/11122158
https://avg.163.com/topic/detail/11122157
https://avg.163.com/topic/detail/11122156
https://avg.163.com/topic/detail/11122155
https://avg.163.com/topic/detail/11122154
https://avg.163.com/topic/detail/11122153
https://avg.163.com/topic/detail/11122152
https://avg.163.com/topic/detail/11122151
https://avg.163.com/topic/detail/11122150
https://avg.163.com/topic/detail/11122149
https://avg.163.com/topic/detail/11122148
https://avg.163.com/topic/detail/11122146
https://avg.163.com/topic/detail/11122145
https://avg.163.com/topic/detail/11122144
https://avg.163.com/topic/detail/11122143
https://avg.163.com/topic/detail/11122142
https://avg.163.com/topic/detail/11122141
https://avg.163.com/topic/detail/11122140
https://avg.163.com/topic/detail/11122139
https://avg.163.com/topic/detail/11122138
https://avg.163.com/topic/detail/11122137
https://avg.163.com/topic/detail/11122136
https://avg.163.com/topic/detail/11122135
https://avg.163.com/topic/detail/11122134
https://avg.163.com/topic/detail/11122133
https://avg.163.com/topic/detail/11122132
https://avg.163.com/topic/detail/11122129
https://avg.163.com/topic/detail/11122131
https://avg.163.com/topic/detail/11122130
https://avg.163.com/topic/detail/11122128
https://avg.163.com/topic/detail/11122127
https://avg.163.com/topic/detail/11122126
https://avg.163.com/topic/detail/11122125
https://avg.163.com/topic/detail/11122124
https://avg.163.com/topic/detail/11122123
https://avg.163.com/topic/detail/11122122
https://avg.163.com/topic/detail/11122121
https://avg.163.com/topic/detail/11122119
https://avg.163.com/topic/detail/11122120
https://avg.163.com/topic/detail/11122117
https://avg.163.com/topic/detail/11122116
https://avg.163.com/topic/detail/11122115
https://avg.163.com/topic/detail/11122114
https://avg.163.com/topic/detail/11122113
https://avg.163.com/topic/detail/11122110
https://avg.163.com/topic/detail/11122112
https://avg.163.com/topic/detail/11122111
https://avg.163.com/topic/detail/11122109
https://avg.163.com/topic/detail/11122108
https://avg.163.com/topic/detail/11122107
https://avg.163.com/topic/detail/11122104
https://avg.163.com/topic/detail/11122102
https://avg.163.com/topic/detail/11122106
https://avg.163.com/topic/detail/11122101
https://avg.163.com/topic/detail/11122105
https://avg.163.com/topic/detail/11122103
https://avg.163.com/topic/detail/11122100
https://avg.163.com/topic/detail/11122099
https://avg.163.com/topic/detail/11122098
https://avg.163.com/topic/detail/11122097
https://avg.163.com/topic/detail/11122095
https://avg.163.com/topic/detail/11122096
https://avg.163.com/topic/detail/11122094
https://avg.163.com/topic/detail/11122093
https://avg.163.com/topic/detail/11122092
https://avg.163.com/topic/detail/11122091
https://avg.163.com/topic/detail/11122090
https://avg.163.com/topic/detail/11122089
https://avg.163.com/topic/detail/11122088
https://avg.163.com/topic/detail/11122086
https://avg.163.com/topic/detail/11122087
https://avg.163.com/topic/detail/11122085
https://avg.163.com/topic/detail/11122084
https://avg.163.com/topic/detail/11122083
https://avg.163.com/topic/detail/11122082
https://avg.163.com/topic/detail/11122081
https://avg.163.com/topic/detail/11122080
https://avg.163.com/topic/detail/11122079
https://avg.163.com/topic/detail/11122078
https://avg.163.com/topic/detail/11122077
https://avg.163.com/topic/detail/11122076
https://avg.163.com/topic/detail/11122075
https://avg.163.com/topic/detail/11122074
https://avg.163.com/topic/detail/11122073
https://avg.163.com/topic/detail/11122072
https://avg.163.com/topic/detail/11122071
https://avg.163.com/topic/detail/11122069
https://avg.163.com/topic/detail/11122070
https://avg.163.com/topic/detail/11122068
https://avg.163.com/topic/detail/11122067
https://avg.163.com/topic/detail/11122066
https://avg.163.com/topic/detail/11122065
https://avg.163.com/topic/detail/11122064
https://avg.163.com/topic/detail/11122063
https://avg.163.com/topic/detail/11122062
https://avg.163.com/topic/detail/11122061
https://avg.163.com/topic/detail/11122060
https://avg.163.com/topic/detail/11122056
https://avg.163.com/topic/detail/11122057
https://avg.163.com/topic/detail/11122059
https://avg.163.com/topic/detail/11122058
https://avg.163.com/topic/detail/11122055
https://avg.163.com/topic/detail/11122054
https://avg.163.com/topic/detail/11122053
https://avg.163.com/topic/detail/11122052
https://avg.163.com/topic/detail/11122051
https://avg.163.com/topic/detail/11122050
https://avg.163.com/topic/detail/11122049
https://avg.163.com/topic/detail/11122048
https://avg.163.com/topic/detail/11122047
https://avg.163.com/topic/detail/11122046
https://avg.163.com/topic/detail/11122045
https://avg.163.com/topic/detail/11122044
https://avg.163.com/topic/detail/11122043
https://avg.163.com/topic/detail/11122042
https://avg.163.com/topic/detail/11122041
实例
#include<iostream> #include <limits> using namespace std; int main() { cout << "type: \\t\\t" << "************size**************"<< endl; cout << "bool: \\t\\t" << "所占字节数:" << sizeof(bool); cout << "\\t最大值:" << (numeric_limits<bool>::max)(); cout << "\\t\\t最小值:" << (numeric_limits<bool>::min)() << 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; cout << "wchar_t: \\t" << "所占字节数:" << sizeof(wchar_t); cout << "\\t最大值:" << (numeric_limits<wchar_t>::max)(); cout << "\\t\\t最小值:" << (numeric_limits<wchar_t>::min)() << endl; cout << "short: \\t\\t" << "所占字节数:" << sizeof(short); cout << "\\t最大值:" << (numeric_limits<short>::max)(); cout << "\\t\\t最小值:" << (numeric_limits<short>::min)() << 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; 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; 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; 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; }
本实例使用了 endl,这将在每一行后插入一个换行符,<< 运算符用于向屏幕传多个值,sizeof() 运算符用来获取各种数据类型的大小。
当上面的代码被编译和执行时,它会产生以下的结果,结果会根据所使用的计算机而有所不同:
type: ************size**************
bool: 所占字节数:1 最大值:1 最小值:0
char: 所占字节数:1 最大值: 最小值:?
signed char: 所占字节数:1 最大值: 最小值:?
unsigned char: 所占字节数:1 最大值:? 最小值:
wchar_t: 所占字节数:4 最大值:2147483647 最小值:-2147483648
short: 所占字节数:2 最大值:32767 最小值:-32768
int: 所占字节数:4 最大值:2147483647 最小值:-2147483648
unsigned: 所占字节数:4 最大值:4294967295 最小值:0
long: 所占字节数:8 最大值:9223372036854775807 最小值:-9223372036854775808
unsigned long: 所占字节数:8 最大值:18446744073709551615 最小值:0
double: 所占字节数:8 最大值:1.79769e+308 最小值:2.22507e-308
long double: 所占字节数:16 最大值:1.18973e+4932 最小值:3.3621e-4932
float: 所占字节数:4 最大值:3.40282e+38 最小值:1.17549e-38
size_t: 所占字节数:8 最大值:18446744073709551615 最小值:0
string: 所占字节数:24
type: ************size**************
网硕互联帮助中心


评论前必须登录!
注册