Re: warning: comparison between signed and unsigned integer expressions
Clark Cox <[email protected]>
| Newsgroups | gmane.comp.macosx.devel |
|---|---|
| Message-ID | <[email protected]> |
On Fri, Apr 23, 2010 at 6:11 AM, Paul Sargent <[email protected]> wrote: > On Tue, Apr 20, 2010 at 23:14, Clark Cox <[email protected]> wrote: >> On Tue, Apr 20, 2010 at 1:59 PM, Stephen Hoffman <[email protected]> wrote: >>> Have a look around for the value-preserving and unsigned-preserving discussions; current C tends to be value-preserving. >> >> The value-preserving behavior is mandated by the C standard wherever possible. > > I feel I should know this, but I had to do a test to find what exactly > you meant by "value-preserving". > > int a; > unsigned int b; > > a = -1; > b = (int) a; > > What is 'b'? > > As it turns out, it's 4294967295 or 0xFFFFFFFF Indeed, as the standard says: 1 When a value with integer type is converted to another integer type other than _Bool, if the value can be represented by the new type, it is unchanged. 2 Otherwise, if the new type is unsigned, the value is converted by repeatedly adding or subtracting one more than the maximum value that can be represented in the new type until the value is in the range of the new type. 3 Otherwise, the new type is signed and the value cannot be represented in it; either the result is implementation-defined or an implementation-defined signal is raised. Your example falls under step #2 > > BTW for the original poster, if all you're doing is equality > comparisons (or not equal) a simple case tends to be fine, just be > careful with < & >. -1 suddenly becomes greater than zero. > -- Clark S. Cox III [email protected]