The C++ standard does not specify the size of integral types in bytes, but it specifies minimum ranges they must be able to hold.
(C ++标准未指定整数类型的大小(以字节为单位),但指定了它们必须能够容纳的最小范围。)
You can infer minimum size in bits from the required range. (您可以从所需范围以位为单位推断最小大小。)
You can infer minimum size in bytes from that and the value of the CHAR_BIT
macro that defines the number of bits in a byte . (您可以从中推断出最小大小(以字节为单位),并可以定义定义字节中位数的CHAR_BIT
宏的值。)
In all but the most obscure platforms it's 8, and it can't be less than 8. That's because it must be large enough to hold "the eight-bit code units of the Unicode UTF-8 encoding form." (在除最晦涩的平台之外的所有平台上,它都是8,并且不能小于8。这是因为它必须足够大,以容纳“ Unicode UTF-8编码形式的八位代码单元”。)
One additional constraint for char
is that its size is always 1 byte, or CHAR_BIT
bits (hence the name).
(char
另一个约束是其大小始终为1个字节,即CHAR_BIT
位(因此而得名)。)
This is stated explicitly in the standard. (这在标准中有明确规定。)
The C standard is a normative reference for the C++ standard, so even though it doesn't state these requirements explicitly, C++ requires the minimum ranges required by the C standard (page 22), which are the same as those from Data Type Ranges on MSDN :
(C标准是C ++标准的规范性参考 ,因此,即使未明确说明这些要求,C ++仍需要C标准所要求的最小范围(第22页),该范围与数据类型范围中的相同。 MSDN :)
-
signed char
: -127 to 127 (note, not -128 to 127; this accommodates 1's-complement and sign-and-magnitude platforms) (signed char
:-127至127(注意,不是-128至127;这可容纳1的补码和符号幅度平台))
-
unsigned char
: 0 to 255 (unsigned char
:0到255)
- "plain"
char
: same range as signed char
or unsigned char
, implementation-defined (“普通” char
:与已signed char
或未unsigned char
相同的范围, 实现定义)
-
signed short
: -32767 to 32767 (signed short
字母:-32767至32767)
-
unsigned short
: 0 to 65535 (unsigned short
:0到65535)
-
signed int
: -32767 to 32767 (signed int
:-32767至32767)
-
unsigned int
: 0 to 65535 (unsigned int
:0到65535)
-
signed long
: -2147483647 to 2147483647 (signed long
:-2147483647至2147483647)
-
unsigned long
: 0 to 4294967295 (unsigned long
:0到4294967295)
-
signed long long
: -9223372036854775807 to 9223372036854775807 (signed long long
:-9223372036854775807至9223372036854775807)
-
unsigned long long
: 0 to 18446744073709551615 (unsigned long long
:0到18446744073709551615)
A C++ (or C) implementation can define the size of a type in bytes sizeof(type)
to any value, as long as
(C ++(或C)实现可以将类型的sizeof(type)
以字节为单位) sizeof(type)
为任何值,只要)
- the expression
sizeof(type) * CHAR_BIT
evaluates to a number of bits high enough to contain required ranges, and (表达式sizeof(type) * CHAR_BIT
计算得出的位数足以包含所需的范围,并且)
- the ordering of type is still valid (eg
sizeof(int) <= sizeof(long)
). (类型的顺序仍然有效(例如, sizeof(int) <= sizeof(long)
)。)
Putting this all together, we are guaranteed that:
(综合所有这些,我们保证:)
-
char
, signed char
, and unsigned char
are at least 8 bits (char
, signed char
和unsigned char
至少为8位)
-
signed short
, unsigned short
, signed int
, and unsigned int
are at least 16 bits (signed short
, unsigned short
,有signed int
和unsigned int
至少为16位)
-
signed long
and unsigned long
are at least 32 bits (signed long
和unsigned long
至少为32位)
-
signed long long
and unsigned long long
are at least 64 bits (signed long long
unsigned long long
和unsigned long long
至少为64位)
No guarantee is made about the size of float
or double
except that double
provides at least as much precision as float
.
(不保证float
或double
的大小,但double
至少提供与float
一样的精度。)
The actual implementation-specific ranges can be found in <limits.h>
header in C, or <climits>
in C++ (or even better, templated std::numeric_limits
in <limits>
header).
(特定于实现的实际范围可以在C的<limits.h>
标头中找到,或在C ++的<climits>
中找到(甚至更好的是,在<limits>
标头中的模板化std::numeric_limits
)。)
For example, this is how you will find maximum range for int
:
(例如,这是您将找到int
最大范围的方法:)
C:
(C:)
#include <limits.h>
const int min_int = INT_MIN;
const int max_int = INT_MAX;
C++ :
(C ++ :)
#include <limits>
const int min_int = std::numeric_limits<int>::min();
const int max_int = std::numeric_limits<int>::max();