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 | |||
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 | ||
浮点型 避免数量级相差巨大的数之间的加减运算 避免等量判断 处理舍入误差问题:1.用双精度浮点数2 用二进制表示十进制3把浮点变量变成整形变量(*10000) | |||
float 专业的解法是这样的: | |||
double | |||
long double | |||
(编译器)可选支持的整数类型 | |||
long long | long long int | signed long long | signed long long int |
unsigned long long | unsigned 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 Output Example c Character a d or i Signed decimal integer 392 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 point 392.65 g Use the shorter of %e or %f 392.65 G Use the shorter of %E or %f 392.65 o Unsigned octal 610 s String of characters sample u Unsigned decimal integer 7235 x Unsigned hexadecimal integer 7fa X Unsigned hexadecimal integer (capital letters) 7FA p Pointer address B800: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 ). - 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.
additional arguments
数据类型长度与取值范围
数据类型的长度和取值范围是与编译器架构相关的。可以使用sizeof 操 作符检测数据类型的长度(通常表述为字节数)。好在许多编译器都将数据类型长度实现为一种标准。整型和浮点型通常是32位,字符是8位,双精度 (double)类型一般是64位。bool类型一般实现为8位,long long类型是64位。头文件”cfloat” (或者 “float.h”)定义了浮点型的长度和取值范围,头文件”climits” (或者 “limits.h”)定义了整型的长度和取值范围。
头文件<limits>定义了数值的取值范围。模板化的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的最大值和最小值。
为了理解复杂声明,请遵循以下三条规则:
-
从变量名开始(上述例子中分别是
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 |