C++数据类型
C++一般会接触到五种C数据类型: void, int, float, double, char.
| 数据类型 | 描述 |
|---|---|
| void | 没有具体的数据类型 |
| int | 整型 |
| float | 浮点型 |
| double | 双精度浮点型 |
| char | 字符 |
作为补充,C++增加了两种类型:bool 和 wchar_t.
| 数据类型 | 描述 |
|---|---|
| bool | 布尔型, true或者false |
| wchar_t | 宽字符 |
[编辑] 类型修饰符
前述类型中的一些可以使用signed, unsigned, short, long等关键字进行修饰。当这些关键字单独使用的时候,(被修饰的)基础数据类型假定为int。以下是可能的数据类型的完整列表(等价的类型列在一行中)。
| 整数类型 | |||
|---|---|---|---|
| bool | |||
| char | |||
| signed char | |||
| unsigned char | |||
| wchar_t | |||
| short | short int | signed short | signed short int |
| unsigned short | unsigned short int | ||
| int | signed | signed int | |
| unsigned | unsigned int | ||
| long | long int | signed long | signed long int |
| unsigned long | unsigned long int | ||
| 浮点型 | |||
| float | |||
| double | |||
| long double | |||
| (编译器)可选支持的整数类型 | |||
| long long | long long int | signed long long | signed long long int |
| unsigned long long | unsigned long long int |
[编辑] 数据类型长度与取值范围
数据类型的长度和取值范围是与编译器架构相关的。可以使用 keywords/sizeof操作符检测数据类型的长度(通常表述为字节数)。好在许多编译器都将数据类型长度实现为一种标准。整型和浮点型通常是32位,字符是8位,双精度(double)类型一般是64位。bool类型一般实现为8位,long long类型是64位。头文件"cfloat" (或者 "float.h")定义了浮点型的长度和取值范围,头文件"climits" (或者 "limits.h")定义了整型的长度和取值范围。
头文件<limits>定义了数值的取值范围。模板化的 /cn/support/types/limits提供了系统相关的C++数据类型的数值范围。请选用适当的方法,同时提供如下表所示的数据类型作为模板参数。请注意,numeric_limits对于用户自定义类型是可以重载的。
| 方法或 常量 |
返回值 | 描述 |
|---|---|---|
| is_specialized | bool | |
| radix | int | base of exponent |
| digits | int | number of radix digits in mantissa |
| digits10 | int | number of base 10 digits in mantissa |
| is_signed | bool | |
| is_integer | bool | |
| is_exact | bool | |
| min() | <type> | smallest number that can be respresented (not the most negative) |
| max() | <type> | largest number |
| epsilon() | <type> | inherent representation error value |
| round_error() | <type> | maximum rounding adjustment possible |
| infinity() | <type> | |
| quiet_NaN() | <type> | invalid number that does not signal floating point error |
| signaling_NaN() | <type> | invalid number that signals floating point error |
| denorm_min() | <type> | |
| min_exponent | int | |
| min_exponent10 | int | |
| max_exponent | int | |
| max_exponent10 | int | |
| has_infinity | bool | |
| has_quiet_NaN | bool | |
| has_signaling_NaN | bool | |
| has_denorm | <type>_denorm_style | |
| has_denorm_loss | bool | |
| is_iec559 | bool | conforms to IEC-559 |
| is_bounded | bool | |
| is_modulo | bool | |
| traps | bool | |
| tinyness_before | bool | |
| round_style | float_round_style { round_to_nearest, ... } |
最常见的用途是越界检查,即检测某种数据型类能够保存的最小值和最大值。以下代码输出了所在系统中short的最大值和最小值。
#include <limits> std::cout << "Maximum short value: " << std::numeric_limits<short>::max() << std::endl; std::cout << "Minimum short value: " << std::numeric_limits<short>::min() << std::endl;
[编辑] 理解数据类型声明
简单的数据类型声明是非常易于理解的:
int i然而,一些极度复杂的类型声明就难于分析了:
double **d[8] // 嗯?... char *(*(**foo [][8])())[] // 啊!foo是什么类型?
为了理解复杂声明,请遵循以下三条规则:
- 从变量名开始(上述例子中分别是d和foo)
- 到数据类型结束(上述例子中分别是double 和 char )
- 先尽量往右看,直到必须往左的时候才往左看。(如:遇到括号)
举例:
| Expression | Meaning |
|---|---|
| double %%**%%d[8]; | |
| |
**d is ... double** |
| |
d is **an array of 8** ... double |
| |
d is an array of 8 **pointer to** ... double |
| |
d is an array of 8 pointer to **pointer to** double |
另一个例子:
| Expression | Meaning |
|---|---|
| char *(*(%%**%%foo [][8])())[] | |
| |
**foo is ... char** |
| |
foo is **an array of** ... char |
| |
foo is an array of **an array of 8** ... char |
| |
foo is an array of an array of 8 **pointer to** ... char |
| |
foo is an array of an array of 8 pointer to **pointer to** ... char |
| |
foo is an array of an array of 8 pointer to pointer to **function returning** ... char |
| |
foo is an array of an array of 8 pointer to pointer to function returning **pointer to** ... char |
| |
foo is an array of an array of 8 pointer to pointer to function returning pointer to **array of** ... char |
| |
foo is an array of an array of 8 pointer to pointer to function returning pointer to array of **pointer to** char |
更详尽的解释,请点击以下链接查看Steve Friedl关于怎样理解C声明的大作: http:www.unixwiz.net/techtips/reading-cdecl.html。