我刚开始阅读 Hacker's Delight并将 abs(-231) 定义为 -231。这是为什么?
我在几个不同的系统上尝试了 printf("%x", abs(0x80000000))
,我在所有这些系统上都返回了 0x80000000。
最佳答案
实际上,在 C 中,行为是未定义的。来自 C99 标准,§7.20.6.1/2:
The
abs
,labs
, andllabs
functions compute the absolute value of an integerj
. If the result cannot be represented, the behavior is undefined.
及其脚注:
The absolute value of the most negative number cannot be represented in two’s complement.
关于c - 为什么 abs(0x80000000) == 0x80000000?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2539178/