Translations of this page?:

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
shortshort intsigned shortsigned short int
unsigned shortunsigned short int
intsignedsigned int
unsignedunsigned int
longlong intsigned longsigned long int
unsigned longunsigned long int
浮点型
float
double
long double
(编译器)可选支持的整数类型
long longlong long intsigned long longsigned long long int
unsigned long longunsigned long long int

数据类型长度与取值范围

数据类型的长度和取值范围是与编译器架构相关的。可以使用sizeof操作符检测数据类型的长度(通常表述为字节数)。好在许多编译器都将数据类型长度实现为一种标准。整型和浮点型通常是32位,字符是8位,双精度(double)类型一般是64位。bool类型一般实现为8位,long long类型是64位。头文件”cfloat” (或者 “float.h”)定义了浮点型的长度和取值范围,头文件”climits” (或者 “limits.h”)定义了整型的长度和取值范围。

头文件<limits>定义了数值的取值范围。模板化的limits提供了系统相关的C++数据类型的数值范围。请选用适当的方法,同时提供如下表所示的数据类型作为模板参数。请注意,numeric_limits对于用户自定义类型是可以重载的。

方法或
常量
返回值描述
is_specializedbool
radixintbase of exponent
digitsintnumber of radix digits in mantissa
digits10intnumber of base 10 digits in mantissa
is_signedbool
is_integerbool
is_exactbool
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_exponentint
min_exponent10int
max_exponentint
max_exponent10int
has_infinitybool
has_quiet_NaNbool
has_signaling_NaNbool
has_denorm<type>_denorm_style
has_denorm_lossbool
is_iec559boolconforms to IEC-559
is_boundedbool
is_modulobool
trapsbool
tinyness_beforebool
round_stylefloat_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是什么类型?

为了理解复杂声明,请遵循以下三条规则:

  1. 从变量名开始(上述例子中分别是dfoo)
  2. 到数据类型结束(上述例子中分别是doublechar )
  3. 先尽量往右看,直到必须往左的时候才往左看。(如:遇到括号)

举例:

ExpressionMeaning
double **d[8];
double **d[8]; d is … double
double **d[8]; d is an array of 8 … double
double **d[8]; d is an array of 8 pointer to … double
double **d[8]; d is an array of 8 pointer to pointer to double

另一个例子:

ExpressionMeaning
char *(*(**foo [][8])())[]
char *(*(**foo [][8])())[] foo is … char
char *(*(**foo [][8])())[] foo is an array of … char
char *(*(**foo [][8])())[] foo is an array of an array of 8 … char
char *(*(**foo [][8])())[] foo is an array of an array of 8 pointer to … char
char *(*(**foo [][8])())[] foo is an array of an array of 8 pointer to pointer to … char
char *(*(**foo [][8])())[] foo is an array of an array of 8 pointer to pointer to function returning … char
char *(*(**foo [][8])())[] foo is an array of an array of 8 pointer to pointer to function returning pointer to … char
char *(*(**foo [][8])())[] foo is an array of an array of 8 pointer to pointer to function returning pointer to array of … char
char *(*(**foo [][8])())[] 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

 
• • • SitemapRecent changesRSScc