本文介绍了C++ 标准规定 int、long 类型的大小是多少?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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++ 的标准吗?

But are there any standards for C++?

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

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

我试图找到可靠的信息,说明charshortintlongdoublefloat(还有其他我没想到的类型)在不同的架构和编译器下.

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.

推荐答案

C++ 标准没有以字节为单位指定整数类型的大小,但它指定了它们必须能够容纳的最小范围.您可以从所需范围推断出最小大小(以位为单位).您可以从中推断出最小大小(以字节为单位)以及 .除了最不起眼的平台外,所有平台都是 8,并且不能低于 8.

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.

char 的另一个限制是它的大小总是 1 个字节,或 CHAR_BIT 位(因此得名).标准中明确规定了这一点.

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

C 标准是 C++ 标准的规范性参考,因此即使它没有明确说明这些要求,C++ 也要求最小范围 C 标准要求(第 22 页),与 Data Type Ranges 中的相同在 MSDN 上:

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:

  1. signed char:-127 到 127(注意,不是 -128 到 127;这适用于 1 的补码和符号和大小平台)
  2. unsigned char:0 到 255
  3. 普通"char:与signed charunsigned char 相同的范围,实现定义
  4. 签名短:-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
  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 char or 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

C++(或 C)实现可以将类型的大小(以字节为单位)sizeof(type) 定义为任何值,只要

A C++ (or C) implementation can define the size of a type in bytes sizeof(type) to any value, as long as

  1. 表达式 sizeof(type) * CHAR_BIT 计算为足够高的位数以包含所需的范围,并且
  2. 类型的排序仍然有效(例如 sizeof(int) ).
  1. the expression sizeof(type) * CHAR_BIT evaluates 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)).

综上所述,我们保证:

  • charsigned charunsigned char 至少为 8 位
  • signed shortunsigned shortsigned intunsigned int 至少为 16 位莉>
  • signed longunsigned long 至少为 32 位
  • signed long longunsigned long long 至少为 64 位
  • char, signed char, and unsigned char are at least 8 bits
  • signed short, unsigned short, signed int, and unsigned int are at least 16 bits
  • signed long and unsigned long are at least 32 bits
  • signed long long and unsigned long long are at least 64 bits

不保证 floatdouble 的大小,除了 double 提供的精度至少与 float.

No guarantee is made about the size of float or double except that double provides at least as much precision as float.

实际实现特定的范围可以在 C 中的 标头或 C++ 中的 中找到(甚至更好,模板化 标头中的 std::numeric_limits).

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).

例如,您将如何找到 int 的最大范围:

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 类型的大小是多少?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 02:35