C++ 标准规定 int、long 类型的大小是多少?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/589575/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 16:07:11  来源:igfitidea点击:

What does the C++ standard state the size of int, long type to be?

c++c++-faq

提问by Jér?me

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.

我正在寻找有关基本 C++ 类型大小的详细信息。我知道这取决于架构(16 位、32 位、64 位)和编译器。

But are there any standards for C++?

但是 C++ 有什么标准吗?

I'm using Visual Studio 2008 on a 32-bit architecture. Here is what I get:

我在 32 位架构上使用 Visual Studio 2008。这是我得到的:

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.

我试图找到在不同架构和编译器下说明char, short, int, long, double, float(以及我没有想到的其他类型)大小的可靠信息,但没有取得多大成功。

回答by 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_BITmacro 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. That's because it must be large enough to hold "the eight-bit code units of the Unicode UTF-8 encoding form."

C++ 标准没有以字节为单位指定整数类型的大小,但它指定了它们必须能够容纳的最小范围。您可以从所需范围推断出最小大小(以位为单位)。您可以从中推断出以字节为单位的最小大小以及定义字节中位数CHAR_BIT宏的值。除了最晦涩的平台,它是 8,并且不能小于 8。这是因为它必须足够大以容纳“Unicode UTF-8 编码形式的八位代码单元”。

One additional constraint for charis that its size is always 1 byte, or CHAR_BITbits (hence the name). This is stated explicitly in the standard.

的另一个限制char是它的大小始终为 1 个字节或CHAR_BIT位(因此得名)。这在标准中有明确规定。

The C standard is a normative referencefor 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 页)要求的最小范围,这些范围与 Data Type Ranges on微软

  1. signed char: -127 to 127 (note, not -128 to 127; this accommodates 1's-complement and sign-and-magnitude platforms)
  2. unsigned char: 0 to 255
  3. "plain" char: same range as signed charor unsigned char, implementation-defined
  4. signed short: -32767 to 32767
  5. unsigned short: 0 to 65535
  6. signed int: -32767 to 32767
  7. unsigned int: 0 to 65535
  8. signed long: -2147483647 to 2147483647
  9. unsigned long: 0 to 4294967295
  10. signed long long: -9223372036854775807 to 9223372036854775807
  11. unsigned long long: 0 to 18446744073709551615
  1. signed char:-127 到 127(注意,不是 -128 到 127;这适用于 1 的补码和符号和大小平台)
  2. unsigned char: 0 到 255
  3. "plain" char: 与signed charor相同的范围unsigned char实现定义
  4. signed short: -32767 到 32767
  5. unsigned short: 0 到 65535
  6. signed int: -32767 到 32767
  7. unsigned int: 0 到 65535
  8. signed long: -2147483647 至 2147483647
  9. unsigned long: 0 到 4294967295
  10. signed long long: -9223372036854775807 至 9223372036854775807
  11. 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)为单位)定义为任何值,只要

  1. the expression sizeof(type) * CHAR_BITevaluates to a number of bits high enough to contain required ranges, and
  2. the ordering of type is still valid (e.g. sizeof(int) <= sizeof(long)).
  1. 表达式sizeof(type) * CHAR_BIT计算为足够高的位数以包含所需的范围,并且
  2. type 的排序仍然有效(例如sizeof(int) <= sizeof(long))。

Putting this all together, we are guaranteed that:

综上所述,我们保证:

  • char, signed char, and unsigned charare at least 8 bits
  • signed short, unsigned short, signed int, and unsigned intare at least 16 bits
  • signed longand unsigned longare at least 32 bits
  • signed long longand unsigned long longare at least 64 bits
  • char, signed char, 和unsigned char至少为 8 位
  • signed short, unsigned short, signed int, 和unsigned int至少为 16 位
  • signed long并且unsigned long至少是 32 位
  • signed long long并且unsigned long long至少是 64 位

No guarantee is made about the size of floator doubleexcept that doubleprovides at least as much precision as float.

不保证float或的大小,doubledouble提供的精度至少与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_limitsin <limits>header).

实际的特定于实现的范围可以<limits.h>在 C 或<climits>C++中的头文件中找到(甚至更好,std::numeric_limits<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();

回答by Jonathan Leffler

For 32-bit systems, the 'de facto' standard is ILP32 — that is, int, longand pointer are all 32-bit quantities.

对于 32 位系统,“事实上的”标准是 ILP32 — 即intlong和 指针都是 32 位数量。

For 64-bit systems, the primary Unix 'de facto' standard is LP64 — longand pointer are 64-bit (but intis 32-bit). The Windows 64-bit standard is LLP64 — long longand pointer are 64-bit (but longand intare both 32-bit).

对于 64 位系统,主要的 Unix '事实上' 标准是 LP64 —long指针是 64 位(但int也是 32 位)。在Windows 64位标准是LLP64 -long long和指针是64位(但longint都是32位)。

At one time, some Unix systems used an ILP64 organization.

曾几何时,一些 Unix 系统使用 ILP64 组织。

None of these de facto standards is legislated by the C standard (ISO/IEC 9899:1999), but all are permitted by it.

这些事实上的标准都不是由 C 标准 (ISO/IEC 9899:1999) 立法的,但都得到了它的允许。

And, by definition, sizeof(char)is 1, notwithstanding the test in the Perl configure script.

而且,根据定义,sizeof(char)is 1,尽管在 Perl 配置脚本中进行了测试。

Note that there were machines (Crays) where CHAR_BITwas much larger than 8. That meant, IIRC, that sizeof(int)was also 1, because both charand intwere 32-bit.

请注意,有些机器(Crays)CHAR_BIT比 8 大得多。这意味着 IIRCsizeof(int)也是 1,因为charint都是 32 位。

回答by John Leidegren

In practice there's no such thing. Often you can expect std::size_tto 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.

在实践中没有这样的事情。通常,您可以期望std::size_t在当前体系结构上表示无符号本机整数大小。即 16 位、32 位或 64 位,但正如对此答案的评论中所指出的那样,情况并非总是如此。

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:

至于所有其他内置类型,它实际上取决于编译器。以下是从最新 C++ 标准的当前工作草案中摘录的两段:

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.

For each of the standard signed integer types, there exists a corresponding (but different) standard unsigned integer type: unsigned char, unsigned short int, unsigned int, unsigned long int, and unsigned long long int, each of which occupies the same amount of storage and has the same alignment requirements.

有五种标准的有符号整数类型:signed char、short int、int、long int 和 long long int。在此列表中,每种类型至少提供与列表中它之前的类型一样多的存储空间。

对于每一种标准的有符号整数类型,都存在相应的(但不同的)标准无符号整数类型:unsigned char、unsigned short int、unsigned int、unsigned long int 和 unsigned long long int,它们各自占用相同的数量存储并具有相同的对齐要求。

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.

如果您愿意,您可以静态(编译时)断言这些基本类型的大小。如果假设的大小发生变化,它会提醒人们考虑移植您的代码。

回答by yinyueyouge

There is standard.

有标准。

C90 standard requires that

C90 标准要求

sizeof(short) <= sizeof(int) <= sizeof(long)

C99 standard requires that

C99 标准要求

sizeof(short) <= sizeof(int) <= sizeof(long) <= sizeof(long long)

Here is the C99 specifications. Page 22 details sizes of different integral types.

这是 C99 规格。第 22 页详细说明了不同积分类型的尺寸。

Here is the int type sizes (bits) for Windows platforms:

这是 Windows 平台的 int 类型大小(位):

Type           C99 Minimum     Windows 32bit
char           8               8
short          16              16
int            16              32
long           32              32
long long      64              64

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:

如果您关心可移植性,或者您希望类型的名称反映大小,则可以查看 header <inttypes.h>,其中提供了以下宏:

int8_t
int16_t
int32_t
int64_t

int8_tis guaranteed to be 8 bits, and int16_tis guaranteed to be 16 bits, etc.

int8_t保证为 8 位,int16_t保证为 16 位,以此类推。

回答by Ben

If you need fixed size types, use types like uint32_t (unsigned integer 32 bits) defined in stdint.h. They are specified in C99.

如果您需要固定大小的类型,请使用stdint.h 中定义的 uint32_t(无符号整数 32 位)等类型。它们在C99中指定。

回答by Brian Neal

Updated: C++11 brought the types from TR1 officially into the standard:

更新:C++11 将 TR1 中的类型正式纳入标准:

  • long long int
  • unsigned long long int
  • 长长整数
  • 无符号长整型

And the "sized" types from <cstdint>

和“大小”类型来自 <cstdint>

  • int8_t
  • int16_t
  • int32_t
  • int64_t
  • (and the unsigned counterparts).
  • int8_t
  • int16_t
  • int32_t
  • int64_t
  • (和未签名的对应物)。

Plus you get:

另外你得到:

  • int_least8_t
  • int_least16_t
  • int_least32_t
  • int_least64_t
  • Plus the unsigned counterparts.
  • int_least8_t
  • int_least16_t
  • int_least32_t
  • int_least64_t
  • 加上未签名的对应物。

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:

这些类型表示至少具有指定位数的最小整数类型。同样,有至少具有指定位数的“最快”整数类型:

  • int_fast8_t
  • int_fast16_t
  • int_fast32_t
  • int_fast64_t
  • Plus the unsigned versions.
  • int_fast8_t
  • int_fast16_t
  • int_fast32_t
  • int_fast64_t
  • 加上未签名的版本。

What "fast" means, if anything, is up to the implementation. It need not be the fastest for all purposes either.

“快速”意味着什么,如果有的话,取决于实现。对于所有目的,它也不必是最快的。

回答by Jér?me Radix

The C++ Standardsays it like this:

C ++标准说,它是这样的:

3.9.1, §2:

3.9.1, §2:

There are five 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. Plain ints have the natural size suggested by the architecture of the execution environment (44); the other signed integer types are provided to meet special needs.

(44) that is, large enough to contain any value in the range of INT_MIN and INT_MAX, as defined in the header <climits>.

有五种有符号整数类型:“signed char”、“short int”、“int”、“long int”和“long long int”。在此列表中,每种类型至少提供与列表中它之前的类型一样多的存储空间。普通整数具有执行环境架构建议的自然大小 (44);提供其他有符号整数类型以满足特殊需要。

(44) 也就是说,足够大以包含 INT_MIN 和 INT_MAX 范围内的任何值,如标题中所定义 <climits>

The conclusion: It depends on which architecture you're working on. Any other assumption is false.

结论:这取决于您正在使用哪种架构。任何其他假设都是错误的。

回答by Emiliano

Nope, there is no standard for type sizes. Standard only requires that:

不,字体大小没有标准。标准只要求:

sizeof(short int) <= sizeof(int) <= sizeof(long int)

The best thing you can do if you want variables of a fixed sizes is to use macros like this:

如果您想要固定大小的变量,您可以做的最好的事情是使用这样的宏:

#ifdef SYSTEM_X
  #define WORD int
#else
  #define WORD long int
#endif

Then you can use WORD to define your variables. It's not that I like this but it's the most portableway.

然后你可以使用 WORD 来定义你的变量。不是我喜欢这个,而是它最便携的方式。

回答by milan-j

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:

在 sizeof(int) == 4 的机器上,我们可以定义:

typedef int int32;

int32 i;
int32 j;
...

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.

因此,当我们将代码转移到实际 long int 的大小为 4 的另一台机器时,我们可以重新定义 int 的单次出现。

typedef long int int32;

int32 i;
int32 j;
...

回答by Crashworks

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.

对于浮点数,有一个标准(IEEE754):浮点数是 32 位,双精度数是 64。这是一个硬件标准,而不是 C++ 标准,因此编译器理论上可以将浮点数和双精度定义为其他大小,但实际上我'从未见过使用任何不同的架构。