警告:此十进制常量仅在ISO C90中无符号 [英] Warning: this decimal constant is unsigned only in ISO C90
问题描述
代码:
long rangeVar = 0;
rangeVar = atol(p_value);
if (rangeVar >= -2147483648 && rangeVar <= 2147483647)
在编译时,我得到:
警告:此十进制常数仅在ISO C90中无符号
warning: this decimal constant is unsigned only in ISO C90
感谢提前
推荐答案
在1990版本中,非固定十进制整数常量的类型是<$ c $的第一个c> int , long int
或 unsigned long int
代表。 (C90没有长长
或 unsigned long long
类型)。
In the 1990 version, an unsuffixed decimal integer constant's type is the first of int
, long int
, or unsigned long int
in which its value can be represented. (C90 had no long long
or unsigned long long
type).
在1999和2011版本中,其类型是 int
, long int
, long long int
;
In the 1999 and 2011 versions, its type is one of int
, long int
, long long int
; it's never of any unsigned type.
特定常数的类型(例如 2147483648
)将根据以下因素而有所不同:您正在使用的编译器的整数类型的范围。如果你的编译器的 long
类型恰好是32位,那么 2147483648
将是 long
如果你的编译器使用C90规则( long long
保证至少为64位)。编译器会警告你这个。
The type of a particular constant (such as 2147483648
) will vary depending on the ranges of the integer types for the compiler you're using. If your compiler's long
type happens to be 32 bits, then 2147483648
will be of type unsigned long
if your compiler uses C90 rules, or of type long long
if it uses C11 rules (long long
is guaranteed to be at least 64 bits). The compiler is warning you about this.
你可以添加后缀来指定常量的类型,但没有后缀。 int
。可以为 unsigned int
, L
添加 U
code> long , UL
for unsigned long,等等。
You can add suffixes to specify the type of a constant -- but there's no suffix for plain signed int
. You can add U
for unsigned int
, L
for long
, UL
for unsigned long, and so forth.
请务必记住, -2147483648
是不是一个整数常数;而 2147483648
本身是一个整数常量, -2147483648
是一个表达式,对该常量应用一元减运算符。根据C90规则,如果常数是 unsigned long
类型,那么这是一个无符号一元减,在无符号算术规则下, code> 2147483648 。根据C99或C11规则, 2147483648
很可能是类型(签名) long long
code> -2147483648 ,以及类型 long long
。
It's important to keep in mind that -2147483648
is not an integer constant; rather 2147483648
by itself is an integer constant, and -2147483648
is an expression that applies a unary minus operator to that constant. Under C90 rules, if the constant is of type unsigned long
, that's an unsigned unary minus, which under the rules of unsigned arithmetic yields the value 2147483648
. Under C99 or C11 rules, 2147483648
is likely to be of type (signed) long long
, and negating it yields -2147483648
, also of type long long
.
有时会看到使用( - 2147483647 - 1)
的代码来避免此问题;给定一个32位 int
, 2147483647
的类型为 int
,并且表达式的结果产生预期的 int
值没有溢出。
You'll sometimes see code that uses (-2147483647 - 1)
to avoid this problem; given a 32-bit int
, 2147483647
is of type int
and the result of the expression yields the expected int
value without overflow.
当然如果你的编译器有不同整数类型的大小,这可能变得更复杂。
Of course if your compiler has different sizes for the integer types, this can become even more complicated.
这篇关于警告:此十进制常量仅在ISO C90中无符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!