为什么INT64_MIN有不同的定义?为什么他们的行为有所不同? [英] Why are there differing definitions of INT64_MIN? And why do they behave differently?
问题描述
我公司的 stdint.h
标题显示为:
#define INT64_MIN -9223372036854775808LL
但是在我的项目的一些代码中,一位程序员写道:
#undef INT64_MIN
#define INT64_MIN(-9223372036854775807LL -1)
然后他在代码中使用这个定义。
该项目编译时没有任何警告/错误。
当我尝试删除他的定义,并使用默认的,我得到:
错误:整数常量是如此之大,它是无符号
这两个定义似乎是等价的。
为什么一个编译好而另一个失败?
-9223372036854775808LL
不是单个文字。它是一个表达式,由应用于常量 9223372036854775808LL
的一元 -
运算符组成。
该常数(很少)超出
另一方面,表达式
( - 9223372036854775807LL -1)
包含的字面值在 long long $ c范围内$ c>,并且对于 INT64_MIN
同样是更有效的定义,因为它是正确的类型(正如Steve Jessop在评论中指出的那样)。
The stdint.h
header at my company reads:
#define INT64_MIN -9223372036854775808LL
But in some code in my project, a programmer wrote:
#undef INT64_MIN
#define INT64_MIN (-9223372036854775807LL -1)
He then uses this definition in the code.
The project compiles with no warnings/errors.
When I attempted to remove his definition and use the default one, I got:
error: integer constant is so large that it is unsigned
The two definitions appear to be equivalent.
Why does one compile fine and the other fails?
解决方案 -9223372036854775808LL
is not a single literal. It's an expression consisting of a unary -
operator applied to the constant 9223372036854775808LL
.
That constant is (barely) outside the range of type long long
, which causes the warning.
The expression (-9223372036854775807LL -1)
, on the other hand, contains literals that are within the range of long long
, and is an equally a more valid definition for INT64_MIN
, since it's of the correct type (as Steve Jessop points out in a comment).
这篇关于为什么INT64_MIN有不同的定义?为什么他们的行为有所不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!