C99 是否强制要求 `int64_t` 类型始终可用?

Does C99 mandate a `int64_t` type be available, always?

C99 标准是否要求符合标准的编译器定义(和可用)64 位 int64_t?或者它是可选的,并且恰好由所有流行的编译器定义?我显然是在专门询问 CPU 无法直接处理 64 位值的平台,但问题更笼统

我无法从 C data types Wikipedia page, nor from this answer 到相关问题中真正弄清楚这一点。

不,C99 不强制要求 int64_t 类型。

(感谢@user3386109,@Clifford)

int64_t 类型不需要可用。引用 C99 standard draft document N1256:

7.18.11.1 Exact-width integer types

  1. The typedef name int N_t designates a signed integer type etc. etc. ...
  2. These types are optional. However, if an implementation provides integer types with widths of ... 64 bits... that have a two’s complement representation... it shall define the corresponding typedef name.

但请参阅@chux 和@JohnBollinger 关于 long long 具有 64 位的答案。

Does the C99 standard mandate that a conforming compiler have a 64-bit int64_t defined (and usable)?

不,但 C99 需要 long long,这是 至少 64 位。

此外,int64_t 非常普遍。如果没有 int64_t,您将不太可能遇到符合标准的 C99,因为几乎所有平台都需要

C11dr 7.20.1.1 Exact-width integer types
(int64_t) .... if an implementation provides integer types with widths of 8, 16, 32, or 64 bits, no padding bits, and (for the signed types) that have a two’s complement representation, it shall define the corresponding typedef names.

处理器的位宽不是此功能的一个因素 - long long 必须存在。如果 long long(或任何标准类型是 64 位 2 的补充),那么 int64_t 也必须 存在。处理器的位宽确实会影响性能


评论强调需要 64 位内存进行编码的 long long,根据规范,只能支持 [-0x7fff-ffff-ffff-ffff ... +0x7fff-ffff-ffff-ffff],比 [-0x8000-0000-0000-0000 ... +0x7fff-ffff-ffff-ffff] 的 int64_t 范围差一格。现在这样的平台是极其罕见的,如果它们存在的话。

共有三组integer types:

  • intN_t — 例如 int64_t 及其未签名的对应物;这些确切类型可能不可用。
  • int_leastN_t——例如int_least64_t;类型 int_least8_tint_least16_tint_least32_tint_least64_t 及其无符号对应类型是必需的——其他类型是可选的。
  • int_fastN_t——例如int_fast64_t;类型 int_fast8_tint_fast16_tint_fast32_tint_fast64_t 是必需的(它们是至少具有给定宽度的最快类型)。

该标准还要求支持 long longlong long 的最小可接受范围至少需要 64 位(参见 §5.2.4.1 Sizes of integer types <limits.h>)。因此,该标准可以合法地要求支持 64 位或更多位的 'least' 和 'fast' 类型——它还要求支持 long long.

过去有计算机使用 36 位字,也有使用 60 位字的计算机。这两者都难以提供(基本上,"couldn't provide")确切的宽度类型,但可以轻松地提供对 'least' 和 'fast' 类型的支持。

标准不强制要求 'exact width types' — 请参阅 §7.20.1.1 Exact-width integer types ¶3:

These types are optional. However, if an implementation provides integer types with widths of 8, 16, 32, or 64 bits, no padding bits, and (for the signed types) that have a two's complement representation, it shall define the corresponding typedef names.

Does the C99 standard mandate that a conforming compiler have a 64-bit int64_t defined (and usable)? Or is it optional, and just happens to be defined by all popular compilers?

类型在某种意义上是可选的,而在另一种意义上是有条件地必需的。具体来说,C99 says

The typedef name intN_t designates a signed integer type with width N , no padding bits, and a two's complement representation. [...]

These types are optional. However, if an implementation provides integer types with widths of 8, 16, 32, or 64 bits, no padding bits, and (for the signed types) that have a two's complement representation, it shall define the corresponding typedef names.

因此,int64_t 是可选的,因为不需要符合要求的实现来提供与 int64_t 的特征完全匹配的任何类型,如果没有,则它不需要(实际上,根据另一节,一定不能)提供类型 int64_t.

C99 确实指定了一种类型 long long int,其所需的最小范围需要至少 64 位宽的表示。现在有可能在某些实现中没有带符号的整数类型 exactly 64 位宽(例如,可能 int 是 24 位,long 48,并且long long96),也有可能存在64值位整数类型,但包含填充位或未用二进制补码表示。这样的实现可以完全符合,但不定义 int64_t。但在实践中,如今并没有任何此类实现得到普遍使用。