c - 为什么-32768 在下面的平台上有 signed long 类型?

Suppose that on a platform, the minimal signed value is −2^15 = −32768 and the maximum value is 2^15 −1 = 32767. The constant 32768 then doesn’t fit into signed and is thus signed long. As a consequence, the expression -32768 has type signed long. Thus the minimal value of the type signed on such a platform cannot be written as a literal constant.

From Modern C by Jens Gustedt.

很容易理解为什么 32768 是 signed long,但为什么 -32768 也是 signed long 而不是 signed,给定最小值 signed -32768 的值?

最佳答案

因为 -32768 是正字面量 32768(它不适合 16 位值,因此必须由下一个更大的类型表示),然后对其应用一元减号(这不会改变类型)。

如果你写,例如( ( - 32767 ) - 1 ),你得到了 int 类型的常量。

https://stackoverflow.com/questions/70054600/

相关文章:

c++ - 从字节初始化一个由 trivially_copyable 但不是 default_con

javascript - 如何在结帐时传递 stripe 上的现有订阅详细信息

function - 关于在函数和宏定义中使用结构文字

c# - 更新 net5.0 .csproj 到 net6.0

java - 在java中对字符串进行排序时如何忽略空格?

r - 在 dplyr 中跨列过滤

apache-kafka - 如何触发Kafka再平衡?

c# - 如何从 C# 中使用具有另一个函数作为参数的 Rust 函数?

reactjs - 导入 nft.storage 时无法解析 ipfs-car/blockstore

android - 如何在Jetpack Compose中实现翻译动画?