警告:这个十进制常量仅在 ISO C90 中是无符号的 [英] Warning: this decimal constant is unsigned only in ISO C90

查看:31
本文介绍了警告:这个十进制常量仅在 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 年和 1999 年版本的 ISO C 标准之间,十进制整数常量的类型规则发生了变化.

The rules for the types of decimal integer constants changed between the 1990 and 1999 editions of the ISO C standard.

在 1990 版本中,无后缀十进制整数常量的类型是 intlong intunsigned long int 中的第一个,其中可以表示它的值.(C90 没有 long longunsigned 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版本中,它的类型是intlong intlong 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 位,那么如果您的编译器使用 C90 规则,则 2147483648 将是 unsigned long 类型,或者是类型long long 如果它使用 C11 规则(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 添加 U,为 long 添加 L,为 unsigned 添加 UL长,等等.

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 类型,那是一个 unsigned 一元减号,在无符号算术规则下产生值 2147483648.在 C99 或 C11 规则下,2147483648 可能是(签名的)long long 类型,否定它会产生 -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 位 int2147483647int 类型,表达式的结果产生预期的 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.

更新:当我写这篇文章时,gcc 的默认方言是 -std=gnu89.从那时起,它已更改为 -std=gnu11(或未发布版本中的 -std=gnu17).我不确定这对警告有何影响.

UPDATE: When I wrote this, the default dialect for gcc was -std=gnu89. Since then, it's been changed to -std=gnu11 (or -std=gnu17 in unreleased versions). I'm not sure how that affects the warning.

这篇关于警告:这个十进制常量仅在 ISO C90 中是无符号的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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