您的位置:首页 > 编程语言 > C语言/C++

C++数据类型

2011-04-08 10:58 99 查看

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。以下是可能的数据类型的完整列表(等价的类型列在一行中)。

整数类型

检查整数除法 防止除0

检查整数溢出(每个值,中间计算值)

sizeof(char[]) = string.length() +1(/0)

sizeof(char*) = 4

printf(“%llu %lli %lld”

bool size 1(可增加boo变量来简化复杂的判断)
char size 1
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
浮点型

避免数量级相差巨大的数之间的加减运算

避免等量判断

处理舍入误差问题:1.用双精度浮点数2 用二进制表示十进制3把浮点变量变成整形变量(*10000)

float 
专业的解法是这样的:

| u - v | / |u| <= e and | u - v | / |v| <= e(1)

| u - v | / |u| <= e or   | u - v | / |v| <= e (2)

(1)式用来判断两个浮点数非常接近

2)式用来判断两个浮点数足够接近

上面的解法是BOOST库中的算法

一般常用的方法是fabs(a-b) <= numeric_limits<float>::epsilon()

或者fabs(a-b)<1e-6,后面的1e-6代表需要到达的小数精度

double
long double
(编译器)可选支持的整数类型
long longlong long intsigned long longsigned long long int
unsigned long longunsigned long long int
int printf ( const char * format, ... );


Print formatted data to stdout

Writes to the standard output (stdout) a sequence of data formatted as the format
argument specifies. After the format
parameter, the function expects at least as many additional arguments as specified in format
.

Parameters

format
String that contains the text to be written to stdout
.

It can optionally contain embedded format tags that are substituted by
the values specified in subsequent argument(s) and formatted as
requested.

The number of arguments following the format
parameters should at least be as much as the number of format tags.

The format tags follow this prototype:

%[flags][width][.precision][length]specifier

Where specifier
is the most significant one and defines the type and the interpretation of the value of the coresponding argument:
specifier
OutputExample
c
Charactera
d
or i
Signed decimal integer392
e
Scientific notation (mantise/exponent) using e
character
3.9265e+2
E
Scientific notation (mantise/exponent) using E
character
3.9265E+2
f
Decimal floating point392.65
g
Use the shorter of %e
or %f
392.65
G
Use the shorter of %E
or %f
392.65
o
Unsigned octal610
s
String of characterssample
u
Unsigned decimal integer7235
x
Unsigned hexadecimal integer7fa
X
Unsigned hexadecimal integer (capital letters)7FA
p
Pointer addressB800:0000
n
Nothing printed. The argument must be a pointer to a signed int
, where the number of characters written so far is stored.
%
A %
followed by another %
character will write %
to stdout
.
%
The tag can also contain flags
, width
, .precision
and modifiers
sub-specifiers, which are optional and follow these specifications:

flags
description
-
Left-justify within the given field width; Right justification is the default (see width
sub-specifier).
+
Forces to precede the result with a plus or minus sign (+
or -
) even for positive numbers. By default, only negative numbers are preceded with a -
sign.
(space)
If no sign is going to be written, a blank space is inserted before the value.
#
Used with o
, x
or X
specifiers the value is preceeded with 0
, 0x
or 0X
respectively for values different than zero.

Used with e
, E
and f
, it forces the written
output to contain a decimal point even if no digits would follow. By
default, if no digits follow, no decimal point is written.

Used with g
or G
the result is the same as with e
or E
but trailing zeros are not removed.
0
Left-pads the number with zeroes (0
) instead of spaces, where padding is specified (see width
sub-specifier).
width
description
(number)
Minimum number of characters to be
printed. If the value to be printed is shorter than this number, the
result is padded with blank spaces. The value is not truncated even if
the result is larger.
*
The width
is not specified in the format
string, but as an additional integer value argument preceding the argument that has to be formatted.
.precision
description
.
number
For integer specifiers (d
, i
, o
, u
, x
, X
): precision
specifies the minimum number of digits to be written. If the value to
be written is shorter than this number, the result is padded with
leading zeros. The value is not truncated even if the result is longer. A
precision
of 0
means that no character is written for the value 0
.

For e
, E
and f
specifiers: this is the number of digits to be printed after
the decimal point.

For g
and G
specifiers: This is the maximum number of significant digits to be printed.

For s
: this is the maximum number of characters to be printed.
By default all characters are printed until the ending null character is
encountered.

For c
type: it has no effect.

When no precision
is specified, the default is 1
. If the period is specified without an explicit value for precision
, 0
is assumed.
.*
The precision
is not specified in the format
string, but as an additional integer value argument preceding the argument that has to be formatted.
length
description
h
The argument is interpreted as a short int
or unsigned short int
(only applies to integer specifiers: i
, d
, o
, u
, x
and X
).
l
The argument is interpreted as a long int
or unsigned long int
for integer specifiers (i
, d
, o
, u
, x
and X
), and as a wide character or wide character string for specifiers c
and s
.
L
The argument is interpreted as a long double
(only applies to floating point specifiers: e
, E
, f
, g
and G
).
additional arguments
Depending on the format
string, the function may expect a sequence of additional arguments, each containing one value to be inserted instead of each %
-tag specified in the format
parameter, if any. There should be the same number of these arguments as the number of %
-tags that expect a value.

数据类型长度与取值范围

数据类型的长度和取值范围是与编译器架构相关的。可以使用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的最大值和最小值。

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

从变量名开始(上述例子中分别是
d

foo

)

到数据类型结束(上述例子中分别是
double

char

)

先尽量往右看,直到必须往左的时候才往左看。(如:遇到括号)

举例:

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息