为什么 INT64_MIN 有不同的定义?为什么他们的行为不同? [英] Why are there differing definitions of INT64_MIN? And why do they behave differently?

查看:25
本文介绍了为什么 INT64_MIN 有不同的定义?为什么他们的行为不同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我公司的 stdint.h 标头如下:

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 不是单个文字.它是一个由应用于常量 9223372036854775808LL 的一元 - 运算符组成的表达式.

-9223372036854775808LL is not a single literal. It's an expression consisting of a unary - operator applied to the constant 9223372036854775808LL.

该常量(几乎)超出了 long long 类型的范围,这会导致警告.(我假设 long long 是 64 位,它几乎可以肯定是.)

That constant is (barely) outside the range of type long long, which causes the warning. (I'm assuming long long is 64 bits, which it almost certainly is.)

另一方面,表达式 (-9223372036854775807LL -1) 包含在 long long 范围内的文字,并且是 /strike> 更有效的 INT64_MIN 定义,因为它的类型正确(正如 Steve Jessop 在评论中指出的那样).

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆