intmax_t 文字的后缀

Suffix for a intmax_t literal

似乎没有 'J' 后缀(a la printf 的 %jd)。

那么,是否保证 LLULL 后缀将适用于 intmax_t 和 uintmax_t 类型?

#include <stdint.h>

intmax_t yuuge = 123456789101112131411516LL;

或者是否存在对于 LL 后缀来说太大的文字?比如说,一个(假设的)系统有 32 位 int,32 位 long,64 位 long long,128 位 intmax_t.

不需要后缀 如果您只想忠实地表示值。 C 语言自动为整型文字赋予正确的类型。只有当你想强制一个文字具有比它由于其值而自然具有的等级更高的类型时才需要后缀(例如 1UL 将值 1 作为 unsigned long 而不是 int,或 -1UL 作为 ULONG_MAX).

的替代表达式

如果您确实想强制文字具有类型 intmax_t,请使用 stdint.h 中的 INTMAX_C() 宏。

it possible that there are literals that are too big for the LL suffix

是的,整型常量如果超出(u)intmax_t的范围就太大了,有没有LL

参见在 C 中分配 128 位整数 类似的问题。


LLLLU 不适用于类型。它们用于整数常量。

LLL 确保常量的 最小 类型。 intmax_t.

没有后缀
123 is an `int`
123L is a `long`
123LL is a `long long`
123456789012345 is a `long long` on OP's hypothetical system even without LL

intmax_t 可能与 long long 具有相同的范围 - 或者可能更宽。 intmax_tlong long 都至少是 64 位。

使用启用警告的编译器,如果常量超过 intmax_t 范围,则会出现警告。示例:

//  warning: integer overflow in expression
intmax_t yuuge1 = (intmax_t)123456*1000000000000000000 + 789101112131411516;

//  warning: overflow in implicit constant conversion [-Woverflow]
intmax_t yuuge2 = 123456789101112131411516;

C 为最大宽度整数常量提供宏

The following macro expands to an integer constant expression having the value specified by its argument and the type intmax_t: C11 §7.20.4.2 1

INTMAX_C(value)

INTMAX_C(value)确实有一个限制

The argument in any instance of these macros shall be an unsuffixed integer constant ... with a value that does not exceed the limits for the corresponding type.

以下内容不满足 64 位 intmax_t 机器上的要求。

// Not so portable code
intmax_t yuuge = INTMAX_C(123456789101112131411516);

#预处理也仅限于intmax_t.

试图在 (u)int64_t 范围之外创建常量的代码很容易出现可移植性问题。为了可移植性,建议使用另一种编码方法(避免使用如此大的常量)。