问题描述
我正在寻找关于基本C ++类型大小的详细信息。
我知道它取决于架构(16位,32位,64位)和编译器。
但是有没有任何C ++标准? / p>
我在32位架构上使用Visual Studio 2008。这是我得到的:
char:1 byte
short:2 bytes
int:4 bytes
long:4 bytes
float:4 bytes
double:8 bytes
我尝试找到可靠的信息,说明 char
, short
c $ c> int , long
, double
, float
在不同的架构和编译器下, C ++标准不指定整数类型的大小(以字节为单位),但它指定它们必须能够保存的最小范围。您可以根据所需范围推断最小大小(以位为单位)。您可以从中推断出最小大小(以字节为单位)和 CHAR_BIT
宏的值,(除了最隐蔽的平台,它是8,它不能小于8)
char
的一个附加约束是它的大小总是为1字节或 CHAR_BIT
位(因此名称)。
最小范围(第22页)是:
和数据类型范围(位于):
-
signed char
:-127到127(注意,不是-128到127;这里包含1的补码和sign-大小平台) -
unsigned char
:0至255 - plain $ c> char :
与或
或unsigned char
=http://stackoverflow.com/q/2397984>实施定义 -
签署短
: - 32767 to 32767 -
无符号short
:0至65535 -
signed int
:-32767至32767 -
unsigned int
:0至65535 -
signed long
:-2147483647 to 2147483647 -
unsigned long
:0 to 4294967295 -
签署长期
:-9223372036854775807至9223372036854775807 -
unsigned long long
:0 to 18446744073709551615
一个类型的大小以字节 sizeof(type)
为任何值,只要
- 表达式
sizeof(type)* CHAR_BIT
会计算出足够高的位数,以包含必要的范围, - 类型的排序仍然有效(例如
sizeof(int)< = sizeof(long)
)。
可以在C中的< limits.h>
头中找到实际的实现特定范围,或者< climits>
在C ++中(或者更好的是,在< limits>
头中的模板 std :: numeric_limits
)。
例如,这将是如何找到 int
的最大范围:
C:
#include< limits.h>
const int min_int = INT_MIN;
const int max_int = INT_MAX;
C ++ :
#include< limits>
const int min_int = std :: numeric_limits< int> :: min();
const int max_int = std :: numeric_limits< int> :: max();
I'm looking for detailed information regarding the size of basic C++ types.I know that it depends on the architecture (16 bits, 32 bits, 64 bits) and the compiler.
But are there any standards for C++?
I'm using Visual Studio 2008 on a 32-bit architecture. Here is what I get:
char : 1 byte
short : 2 bytes
int : 4 bytes
long : 4 bytes
float : 4 bytes
double: 8 bytes
I tried to find, without much success, reliable information stating the sizes of char
, short
, int
, long
, double
, float
(and other types I didn't think of) under different architectures and compilers.
The C++ standard does not specify the size of integral types in bytes, but it specifies minimum ranges they must be able to hold. 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 (in all but the most obscure platforms it's 8, and it can't be less than 8).
One additional constraint for char
is that its size is always 1 byte, or CHAR_BIT
bits (hence the name).
Minimum ranges required by the standard (page 22) are:
and Data Type Ranges on MSDN:
signed char
: -127 to 127 (note, not -128 to 127; this accommodates 1's-complement and sign-and-magnitude platforms)unsigned char
: 0 to 255- "plain"
char
: same range assigned char
orunsigned char
, implementation-defined signed short
: -32767 to 32767unsigned short
: 0 to 65535signed int
: -32767 to 32767unsigned int
: 0 to 65535signed long
: -2147483647 to 2147483647unsigned long
: 0 to 4294967295signed long long
: -9223372036854775807 to 9223372036854775807unsigned long long
: 0 to 18446744073709551615
A C++ (or C) implementation can define the size of a type in bytes sizeof(type)
to any value, as long as
- the expression
sizeof(type) * CHAR_BIT
evaluates to a number of bits high enough to contain required ranges, and - the ordering of type is still valid (e.g.
sizeof(int) <= sizeof(long)
).
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).
For example, this is how you will find maximum range for int
:
C:
#include <limits.h>
const int min_int = INT_MIN;
const int max_int = INT_MAX;
C++:
#include <limits>
const int min_int = std::numeric_limits<int>::min();
const int max_int = std::numeric_limits<int>::max();
这篇关于什么是C ++标准陈述int,long类型的大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!