為什么INT64_MIN有不同的定義?為什么他們的行為不同?

[英]Why are there differing definitions of INT64_MIN? And why do they behave differently?


The stdint.h header at my company reads:

stdint。h在我公司的標題是:

#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?

這兩個定義似乎是等價的。為什么一個編譯得很好,另一個卻失敗了?

1 个解决方案

#1


15  

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

-9223372036854775808LL不是一個單一的文字。它是一個由一元運算符組成的表達式,應用於常數9223372036854775808LL。

That constant is (barely) outside the range of type long long, which causes the warning.

這個常數(幾乎)超出了long類型的范圍,這導致了警告。

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).

另一方面,表達式(-9223372036854775807LL -1)包含了在長長長范圍內的文字,並且是INT64_MIN的更有效定義,因為它是正確的類型(Steve Jessop在評論中指出)。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2012/06/29/72f6827e192348c0f4c347e2fa4edc78.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com