operators and type promotion

S

swansnow

This is perhaps a basic question, but it's been a while since I've
done C, and none of the searches I did on operators and data types
wanted to address this, so here goes...

In the glibc (linux) library, I found this expression , and I want to
know exactly what it's doing.

res >= 0xfffff001u

Here, res is an int. It looks to me like res is being interpreted as
an unsigned int (long?) and then being compared against the constant
(-4095). So if res happens to be, say, -22, this expression will be
true. If res happens to be -4096, this statement will be false, maybe?

Is that right?

:)

-Corinna
 
J

James Kuyper

swansnow said:
This is perhaps a basic question, but it's been a while since I've
done C, and none of the searches I did on operators and data types
wanted to address this, so here goes...

In the glibc (linux) library, I found this expression , and I want to
know exactly what it's doing.

res >= 0xfffff001u

Here, res is an int. It looks to me like res is being interpreted as
an unsigned int (long?) and then being compared against the constant
(-4095). So if res happens to be, say, -22, this expression will be
true. If res happens to be -4096, this statement will be false, maybe?

Is that right?

It depends very much upon the value of INT_MAX and UINT_MAX. If INT_MAX
> 0xfffff001u, then the constant will be promoted to an int before
performing the comparison. As a result, the comparison will be done as a
signed comparison, and only very large positive values of res will pass
the test. If, on the other hand, INT_MAX < 0xfffff001u, then res will be
converted to the same type as the constant (which will be either
unsigned int or unsigned long, depending upon whether or not UINT_MAX >
0xfffff01u). The exact result you get will depend upon the results of
that conversion.
 
P

Peter Nilsson

If 0xfffff001u <= UINT_MAX, it's equivalent to...

(unsigned) res >= 0xfffff001u

Otherwise it's equivalent to...

(unsigned long) res >= 0xfffff001u
It depends very much upon the value of INT_MAX and
UINT_MAX.

I think you've misremembered.
If INT_MAX > 0xfffff001u, then the constant will be
promoted to an int before performing the comparison.

Not according to 6.4.4.1p5. The u suffix means it is
an unsigned integer; at least an unsigned int, but
possibly a wider (higher rank). It's certainly not a
lower rank than unsigned int though. E.g. (1 > -1u)
is never true.
 
J

James Kuyper

Peter Nilsson wrote:
....
I think you've misremembered.

Yes, you're right. I actually thought to myself "I'd better double check
before I post", and then decided not to. :-(
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,526
Members
44,997
Latest member
mileyka

Latest Threads

Top