C++什么是什么标准说int的大小,长吗?
我正在寻找有关基本C ++类型大小的详细信息。 我知道这取决于体系结构(16位,32位,64位)和编译器。
但是有C ++的标准吗?
我在32位体系结构上使用Visual Studio 2008。这是我得到的:
char : 1 byte
short : 2 bytes
int : 4 bytes
long : 4 bytes
float : 4 bytes
double: 8 bytes
我试图在没有成功的情况下找到可靠的信息,说明 char
的大小,简短
, int
, Long
, double
, float
(以及我想不到的其他类型)在不同的体系结构和编译器下。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(24)
C ++标准未指定字节中积分类型的大小,但它指定位中的最小 width (请参阅 [basic.types.fundamental] p1 )。您可以从该字节中推断出最小尺寸,以及
char_bit
宏观的值-A-BYTE-IN-ANSI-C89-90-C/437640#437640“>定义字节中的位数。除了最晦涩的平台外,它是8个,它不能少于8。位(因此名称)。参见 [expr.sizeof] p1 。
宽度在 [tab:basic.fundamental.width] 。
最小/最大范围是以下结果:
签名char
unsigned char
chod> char
char签名的简短
无符号短
签名int
>无符号int
char> char
的范围是签名>签名的char
或未签名的char
,取决于哪个是char
的基础类型(请参阅 [basic.fundamentaltal> P7 )。历史注:在C ++ 20之前,不能保证签名整数的补充,最低限度更大。例如,
签名的char
的最低限度为-127
或更低。在C ++ 11之前,通过C标准定义宽度。a c ++(或c)实现可以将字节中的类型大小定义为
sizef(type)
只要sizeof(type) * char_bit
评估到许多位高足以包含所需范围的位,并且size> sizef(int)< = sizeof(long)
)。没有保证
float
或double
的大小,除了double
至少提供的精度与float
至少具有一样多的精度。 。实际实现特定范围可以在C中的
< limits.h>
标题中找到,或者< gligits>
在C ++中(甚至更好,甚至更好,模板)
)。< limits>
标题中的std :: numeric_limits例如,这是您如何找到
int
的最大范围:c:
c ++ :
The C++ standard does not specify the size of integral types in bytes, but it specifies a minimum width in bits (see [basic.types.fundamental] p1). 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, orCHAR_BIT
bits (hence the name). See [expr.sizeof] p1.The widths are specified in [tab:basic.fundamental.width].
Minimum/maximum ranges are a consequence of that:
signed char
unsigned char
char
signed short
unsigned short
signed int
unsigned int
signed long
unsigned long
signed long long
unsigned long long
The range of
char
is either that ofsigned char
orunsigned char
, depending on which is the underlying type ofchar
(see [basic.fundamental] p7).Historical note: Before C++20, two's complement wasn't guaranteed for signed integers, and the minimum was one greater. For example, the minimum of
signed char
was-127
or lower. Before C++11, the widths were defined through the C standard.A C++ (or C) implementation can define the size of a type in bytes
sizeof(type)
to any value, as long assizeof(type) * CHAR_BIT
evaluates to a number of bits high enough to contain required ranges, andsizeof(int) <= sizeof(long)
).No guarantee is made about the size of
float
ordouble
except thatdouble
provides at least as much precision asfloat
.The actual implementation-specific ranges can be found in
<limits.h>
header in C, or<climits>
in C++ (or even better, templatedstd::numeric_limits
in<limits>
header).For example, this is how you will find maximum range for
int
:C:
C++:
对于32位系统,“事实”标准是ILP32 - 即,
int
,long
和指针都是32位的数量。对于64位系统,主UNIX“事实上”标准是LP64 -
长
,指针为64位(但是int
是32位)。 Windows 64位标准是LLP64 -长长
,指针为64位(但是long
和int
均为32位)。一次,一些UNIX系统使用了ILP64组织。
这些事实上的标准均未由C标准(ISO/IEC 9899:1999)立法,但所有这些标准都允许所有标准。
而且,根据定义,尽管Perl Configure脚本进行了测试,但
sizef(char)
是1
。请注意,
char_bit
大于8。 和int
是32位。For 32-bit systems, the 'de facto' standard is ILP32 — that is,
int
,long
and pointer are all 32-bit quantities.For 64-bit systems, the primary Unix 'de facto' standard is LP64 —
long
and pointer are 64-bit (butint
is 32-bit). The Windows 64-bit standard is LLP64 —long long
and pointer are 64-bit (butlong
andint
are both 32-bit).At one time, some Unix systems used an ILP64 organization.
None of these de facto standards is legislated by the C standard (ISO/IEC 9899:1999), but all are permitted by it.
And, by definition,
sizeof(char)
is1
, notwithstanding the test in the Perl configure script.Note that there were machines (Crays) where
CHAR_BIT
was much larger than 8. That meant, IIRC, thatsizeof(int)
was also 1, because bothchar
andint
were 32-bit.实际上,没有这样的事情。通常,您可以期望
std :: size_t
表示当前体系结构上未签名的本机整数大小。即16位,32位或64位,但并非总是如此。就所有其他内置类型而言,这实际上取决于编译器。这是从最新C ++标准的当前工作草案中获取的两个摘录:
如果您愿意,可以在静态(编译时)主张这些基本类型的大小。如果假设的大小变化,它将提醒人们考虑移植您的代码。
In practice there's no such thing. Often you can expect
std::size_t
to represent the unsigned native integer size on current architecture. i.e. 16-bit, 32-bit or 64-bit but it isn't always the case as pointed out in the comments to this answer.As far as all the other built-in types go, it really depends on the compiler. Here's two excerpts taken from the current working draft of the latest C++ standard:
If you want to you can statically (compile-time) assert the sizeof these fundamental types. It will alert people to think about porting your code if the sizeof assumptions change.
有标准。
C90标准要求
C99标准要求
。 Page 22详细信息不同的积分类型的大小。
这是Windows平台的int类型尺寸(位):
如果您关注可移植性,或者想要该类型的名称反映大小,则可以查看标题
&lt; inttypes.h&gt;
,有以下宏可用:int8_t
保证为8位,并且保证int16_t
是16位,等等。There is standard.
C90 standard requires that
C99 standard requires that
Here is the C99 specifications. Page 22 details sizes of different integral types.
Here is the int type sizes (bits) for Windows platforms:
If you are concerned with portability, or you want the name of the type reflects the size, you can look at the header
<inttypes.h>
, where the following macros are available:int8_t
is guaranteed to be 8 bits, andint16_t
is guaranteed to be 16 bits, etc.如果您需要固定尺寸类型,请使用。它们在
If you need fixed size types, use types like uint32_t (unsigned integer 32 bits) defined in stdint.h. They are specified in C99.
更新:C ++ 11将TR1的类型正式带入标准:
和
&lt; cstdint&gt;
另外,您得到的:
这些类型代表至少指定数量的最小整数类型。同样,有“最快”整数类型至少具有指定数量的位:
“快速”的意思(如果有的话)取决于实施。对于所有目的,它也不是最快的。
Updated: C++11 brought the types from TR1 officially into the standard:
And the "sized" types from
<cstdint>
Plus you get:
These types represent the smallest integer types with at least the specified number of bits. Likewise there are the "fastest" integer types with at least the specified number of bits:
What "fast" means, if anything, is up to the implementation. It need not be the fastest for all purposes either.
3.9.1,§2:
结论:这取决于您正在研究的架构。任何其他假设都是错误的。
The C++ Standard says it like this:
3.9.1, §2:
The conclusion: It depends on which architecture you're working on. Any other assumption is false.
不,没有类型尺寸的标准。标准只要求:
可以做的最好的事情
如果您想要固定尺寸的变量是使用这样的宏:那么您可以使用Word来定义变量, 。这不是我喜欢这个,而是最便携式的方式。
Nope, there is no standard for type sizes. Standard only requires that:
The best thing you can do if you want variables of a fixed sizes is to use macros like this:
Then you can use WORD to define your variables. It's not that I like this but it's the most portable way.
对于浮点数,有一个标准(ieeee754):floats是32位,双重和双打为64。这是一个硬件标准,而不是C ++标准,因此编译器可以从理论上定义浮点并加倍到其他尺寸,但是实际上,我从未见过使用任何不同的体系结构。
For floating point numbers there is a standard (IEEE754): floats are 32 bit and doubles are 64. This is a hardware standard, not a C++ standard, so compilers could theoretically define float and double to some other size, but in practice I've never seen an architecture that used anything different.
我们可以定义该类型的同义词,以便我们可以创建自己的“标准”。
在一台尺寸(int)== 4的机器上,我们可以定义:
因此,当我们将代码转移到另一个机器中,其中长Int的大小为4时,我们可以重新定义int的单个出现。
We are allowed to define a synonym for the type so we can create our own "standard".
On a machine in which sizeof(int) == 4, we can define:
So when we transfer the code to a different machine where actually the size of long int is 4, we can just redefine the single occurrence of int.
有一个标准,并在各种标准文档(ISO,ANSI和Whatnot)中指定。
Wikipedia有一个很好的页面,解释了它们可能存储的各种类型和最大值:
“ noreferrer”> Integer in Computer Science。
,即使使用标准的C ++编译器,您也可以轻松地找到相关的编译器使用以下代码段:
可以在。它包括您可以调用的其他大量命令以查找各种限制。这可以与传达大小的任何任意类型一起使用,例如std :: streamsize。
约翰的答案包含了最佳描述,因为保证了这些描述。无论您使用哪个平台,都有另一个好的页面更详细地详细介绍每种类型必须包含多少位: int类型,在标准中定义。
我希望这会有所帮助!
There is a standard and it is specified in the various standards documents (ISO, ANSI and whatnot).
Wikipedia has a great page explaining the various types and the max they may store:
Integer in Computer Science.
However even with a standard C++ compiler you can find out relatively easily using the following code snippet:
Documentation for std::numeric_limits can be found at Roguewave. It includes a plethora of other commands you can call to find out the various limits. This can be used with any arbitrary type that conveys size, for example std::streamsize.
John's answer contains the best description, as those are guaranteed to hold. No matter what platform you are on, there is another good page that goes into more detail as to how many bits each type MUST contain: int types, which are defined in the standard.
I hope this helps!
当涉及到不同体系结构和不同编译器的类型时,只需使用编译器在架构上运行以下代码,以查看其输出的内容。下面显示了我的
“有五种标准的整数类型:签名的char,short int,int,int,long int,long int and long int。在此列表中,每种类型都提供至少与列表中的存储一样多。”
When it comes to built in types for different architectures and different compilers just run the following code on your architecture with your compiler to see what it outputs. Below shows my Ubuntu 13.04 (Raring Ringtail) 64 bit g++4.7.3 output. Also please note what was answered below which is why the output is ordered as such:
"There are five standard signed integer types: signed char, short int, int, long int, and long long int. In this list, each type provides at least as much storage as those preceding it in the list."
1)表N1中的表“ 64位程序开发的遗忘问题“
2)” 数据模型”
1) Table N1 in article "The forgotten problems of 64-bit programs development"
2) "Data model"
您可以使用:
datatype = int
,long int
等。您将能够看到输入的任何数据类型的大小。
You can use:
datatype = int
,long int
etc.You will be able to see the size for whichever datatype you type.
如前所述,大小应反映当前的体系结构。如果您想查看当前的编译器如何处理事情,则可以在
limits.h
中达到顶峰。As mentioned the size should reflect the current architecture. You could take a peak around in
limits.h
if you want to see how your current compiler is handling things.如果您对纯C ++解决方案感兴趣,我使用模板和C ++标准代码来根据其位大小在编译时间定义类型。
这使得解决方案可在编译器跨编译器。
背后的想法非常简单:创建一个包含类型的char,int,short,long,long(签名和无符号版本)的列表,以及扫描列表,并使用numeric_limits模板使用给定尺寸的类型选择类型。
包括此标头,您获得了8型stdtype :: int8,stdtype :: int16,stdtype :: int32,stdtype :: int64,stdtype :: uint8,stdtype :: uint16,uint16,uint16,stdtype :: :: :: :: uint32,uint32,uint32,stdtype :: uint :: uint64。
如果无法表示某种类型,它将被评估为STDTYPE :: NULL_TYPE在该标题中也声明。
下面的代码无保修,请仔细检查。
我也是元编程的新手,请随时编辑和更正此代码。
用DEVC ++测试(因此3.5左右的GCC版本)
If you are interested in a pure C++ solution, I made use of templates and only C++ standard code to define types at compile time based on their bit size.
This make the solution portable across compilers.
The idea behind is very simple: Create a list containing types char, int, short, long, long long (signed and unsigned versions) and the scan the list and by the use of numeric_limits template select the type with given size.
Including this header you got 8 type stdtype::int8, stdtype::int16, stdtype::int32, stdtype::int64, stdtype::uint8, stdtype::uint16, stdtype::uint32, stdtype::uint64.
If some type cannot be represented it will be evaluated to stdtype::null_type also declared in that header.
THE CODE BELOW IS GIVEN WITHOUT WARRANTY, PLEASE DOUBLE CHECK IT.
I'M NEW AT METAPROGRAMMING TOO, FEEL FREE TO EDIT AND CORRECT THIS CODE.
Tested with DevC++ (so a gcc version around 3.5)
正如其他人所回答的那样,“标准”都将大多数细节都留为“实现定义”,并且只有该类型的“ char”在leat“ char_bis”宽,而“ char&lt; = short&lt; = short&lt; = int = int&lt; = long&lt; = long long'(浮点和双重与IEEE浮点标准几乎一致,而长双人通常与双重相同 - 但在更新的当前实现中可能更大)。
没有非常具体和精确值的部分原因是因为C/C ++等语言被设计为可移植到大量硬件平台上 - 包括“ char”单词大小可能为4位的计算机系统或7位,甚至是“ 8-/16-/32-/64位”计算机外的普通家庭用户接触到的“ 8-/16-/32-/64位”的其他值。 (这里的单词大小,这意味着系统通常运行的系统宽度 - 与家用计算机用户可能期望的那样,它并不总是8位。)
如果您真的需要一个对象特定数量位的积分值),大多数编译器都有某种方法来指定这一点;但是,即使是AME公司制作的编译器,但对于不同的平台,它通常也不是便携式的。某些标准和实践(尤其是Limits.H等)很常见,以至于大多数编译器都将支持在特定值范围内确定最佳拟合类型,但不使用所使用的位数。 (也就是说,如果您知道需要保持0到127之间的值,则可以确定编译器支持一种“ INT8”类型的8位类型,该类型将足够大,可以保持所需的整个范围,但不像“ INT7”类型,它将是7位的确切匹配。)
注意:许多UN*X源软件包使用“ ./configure”脚本,该脚本将探测编译器/系统的功能并输出合适的makefile和config.h。您可能会检查其中一些脚本,以了解它们的工作原理以及如何探究Comiler/System功能,并遵循其领导。
As others have answered, the "standards" all leave most of the details as "implementation defined" and only state that type "char" is at leat "char_bis" wide, and that "char <= short <= int <= long <= long long" (float and double are pretty much consistent with the IEEE floating point standards, and long double is typically same as double--but may be larger on more current implementations).
Part of the reasons for not having very specific and exact values is because languages like C/C++ were designed to be portable to a large number of hardware platforms--Including computer systems in which the "char" word-size may be 4-bits or 7-bits, or even some value other than the "8-/16-/32-/64-bit" computers the average home computer user is exposed to. (Word-size here meaning how many bits wide the system normally operates on--Again, it's not always 8-bits as home computer users may expect.)
If you really need a object (in the sense of a series of bits representing an integral value) of a specific number of bits, most compilers have some method of specifying that; But it's generally not portable, even between compilers made by the ame company but for different platforms. Some standards and practices (especially limits.h and the like) are common enough that most compilers will have support for determining at the best-fit type for a specific range of values, but not the number of bits used. (That is, if you know you need to hold values between 0 and 127, you can determine that your compiler supports an "int8" type of 8-bits which will be large enought to hold the full range desired, but not something like an "int7" type which would be an exact match for 7-bits.)
Note: Many Un*x source packages used "./configure" script which will probe the compiler/system's capabilities and output a suitable Makefile and config.h. You might examine some of these scripts to see how they work and how they probe the comiler/system capabilities, and follow their lead.
我注意到,这里的所有其他答案几乎完全集中在整体类型上,而发问者还询问了有关浮点的信息。
我认为C ++标准不需要它,但是如今最常见的平台的编译器通常遵循IEEE754浮点数的IEEE754标准。该标准指定了四种类型的二进制浮点(以及一些BCD格式,我从未在C ++编译器中看到过支持):
双精度(二进制64 也是符合标准的其他尺寸。这些称为“扩展类型”。一个常见的例子是一个80位浮点,具有64位的显着性和指数范围为-16382至16383。过去也进行了40位浮点。
那么,如何将其映射到C ++类型上?通常,
float
使用单个精度;因此,sizeof(float)= 4
。然后double
使用双重精度(我相信这是名称double
)的来源,而长
通常是双重精度或80位精度(在我的系统上是80位,但在32位系统上可能是两倍)。从理论上讲,它也可能是四倍的精度。许多编译器还提供一半的精度浮点数作为扩展名,例如clang上的_Float16
。总而言之,这是通常的:
sizeof(float)
= 4sizeof(double)
= 8sizeof(long double)
= 8或16I notice that all the other answers here have focused almost exclusively on integral types, while the questioner also asked about floating-points.
I don't think the C++ standard requires it, but compilers for the most common platforms these days generally follow the IEEE754 standard for their floating-point numbers. This standard specifies four types of binary floating-point (as well as some BCD formats, which I've never seen support for in C++ compilers):
There are also other sizes that conform to the standard. These are called "extended types". A common example is an 80-bit float with a 64-bit significand and an exponent range of -16382 to 16383. A 40-bit float has also been done in the past.
How does this map onto C++ types, then? Generally the
float
uses single precision; thus,sizeof(float) = 4
. Thendouble
uses double precision (I believe that's the source of the namedouble
), andlong double
is usually either double precision or 80-bit precision (it's 80-bit on my system, but on 32-bit systems it may be double). In theory, it could also be quadruple precision. Many compilers also offer half precision floating-points as an extension, for example_Float16
on clang.In summary, this is the usual:
sizeof(float)
= 4sizeof(double)
= 8sizeof(long double)
= 8 or 16从Alex B中,C ++标准并未指定字节中积分类型的大小,但指定了它们必须能够保留的最小范围。您可以从所需范围内推断出最小尺寸。您可以从该字节中推断出最小尺寸,以及定义字节中的位数的char_bit宏的值(除了最晦涩的平台外,它是8个,并且不能少于8)。
char的另一个约束是其大小始终是1个字节或char_bit lit(因此名称)。
标准要求(第22页)所需的最小范围为:
MSDN上的数据类型范围:
签名char:-127至127(注意,不是-128至127;这可容纳1's -complement平台)
未签名的char:0至255
“普通” char:-127至127或0至255(取决于默认的char签名)
签名短:-32767至32767
未签名短:0至65535
签名INT:-32767至32767
未签名INT:0至65535
签名长:-2147483647至2147483647
未签名长:0至4294967295
长长签名:-922372036854775807至9223372036854775807
无签名长:0到184444444073709551615
C ++(或C)实现可以将字节大小(类型)中的类型大小定义为任何值,只要
表达式sizeof(type) * char_bit评估到足以包含所需范围的位数的数量,并且
类型的排序仍然有效(例如sizeof(int)&lt; = sizeof(long))。
实际实现特定的范围可以在C中的标头或C ++中找到(甚至更好的,甚至更好的STD :: Numeric_limits)。
例如,这是您如何找到INT的最大范围:
C:
C ++:
这是正确的,但是,您也是正确的说:
char:1字节
简:2个字节
int:4个字节
长:4个字节
浮点:4个字节
double:8字节,
因为32位体系结构仍然是默认和大多数使用的,并且自32位之前的内存较少时,它们一直保持这些标准尺寸,并且对于向后兼容和标准化,它们保持不变。即使是64位系统也倾向于使用这些并进行范围/修改。
请参考此信息以获取更多信息:
http://en.cppreference.com/w/cpp/语言/类型
From Alex B 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 platforms)
unsigned char: 0 to 255
"plain" char: -127 to 127 or 0 to 255 (depends on default char signedness)
signed short: -32767 to 32767
unsigned short: 0 to 65535
signed int: -32767 to 32767
unsigned int: 0 to 65535
signed long: -2147483647 to 2147483647
unsigned long: 0 to 4294967295
signed long long: -9223372036854775807 to 9223372036854775807
unsigned 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 the number of bits 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 header in C, or in C++ (or even better, templated std::numeric_limits in header).
For example, this is how you will find maximum range for int:
C:
C++:
This is correct, however, you were also right in saying that:
char : 1 byte
short : 2 bytes
int : 4 bytes
long : 4 bytes
float : 4 bytes
double : 8 bytes
Because 32 bit architectures are still the default and most used, and they have kept these standard sizes since the pre-32 bit days when memory was less available, and for backwards compatibility and standardization it remained the same. Even 64 bit systems tend to use these and have extentions/modifications.
Please reference this for more information:
http://en.cppreference.com/w/cpp/language/types
正如您提到的 - 这在很大程度上取决于编译器和平台。为此,检查ANSI标准, http://home.att.net/ jackklein/c/inttypes.html
这是Microsoft编译器的一个:
As you mentioned - it largely depends upon the compiler and the platform. For this, check the ANSI standard, http://home.att.net/~jackklein/c/inttypes.html
Here is the one for the Microsoft compiler: Data Type Ranges.
其中
x
是char
,int
,long
等。 /代码>在位。where
X
is achar
,int
,long
etc.. will give you size ofX
in bits.您可以使用库提供的变量,例如 opentgl ,等。
en.wikipedia.org/wiki/qt_%28software%29“ rel =“ nofollow noreferrer”> qt qtglobal.html“ rel =“ nofollow noreferrer”>提供 qint8(保证在QT支持的所有平台上是8位),Qint16,Qint32,Qint64,Qint64,Quint8,Quint8,Quint8,Quint16,Quint16,Quint32,Quint32,Quint64等
You can use variables provided by libraries such as OpenGL, Qt, etc.
For example, Qt provides qint8 (guaranteed to be 8-bit on all platforms supported by Qt), qint16, qint32, qint64, quint8, quint16, quint32, quint64, etc.
在64位机器上:
On a 64-bit machine:
整数有四种类型的基于大小:
There are four types of integers based on size: