Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
688 views
in Technique[技术] by (71.8m points)

c - Warning: this decimal constant is unsigned only in ISO C90

Piece of code :

long rangeVar = 0;
rangeVar = atol(p_value);

if (rangeVar >= -2147483648 && rangeVar <= 2147483647)

On compiling I get:

warning: this decimal constant is unsigned only in ISO C90

Thanks in Advance

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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

In the 1999 and 2011 versions, its type is one of int, long int, long long int; it's never of any unsigned type.

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.

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.

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.

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.

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...