Access the answers to your technology questions today.
Subscribe Now
30-day free trial. Register in 60 seconds.
What Makes Experts Exchange Unique?
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.
Try it out and discover for yourself.
Subscribe Now
30-day free trial. Register in 60 seconds.
Join the Community
Give a Little. Get a Lot.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Join the Community
by: Infinity08Posted on 2006-01-12 at 00:39:16ID: 15679420
That's because the compiler follows the C99 standard rules of integer promotion :
1) If both operands have the same type, then no further conversion is needed.
2) Otherwise, if both operands have signed integer types or both have unsigned integer types, the operand with the type of lesser integer conversion rank is converted to the type of the operand with greater rank.
3) Otherwise, if the operand that has unsigned integer type has rank greater or equal to the rank of the type of the other operand, then the operand with signed integer type is converted to the type of the operand with unsigned integer type.
4) Otherwise, if the type of the operand with signed integer type can represent all of the values of the type of the operand with unsigned integer type, then the operand with unsigned integer type is converted to the type of the operand with signed integer type.
5) Otherwise, both operands are converted to the unsigned integer type corresponding to the type of the operand with signed integer type.
In this case rule 1 doesn't apply since both operands are of different type (signed int and unsigned int). Rule 2 doesn't apply either since we have both a signed and an unsigned integer type. Rule 3 does apply, as the rank of signed int and unsigned int is the same.
So, the signed integer is converted to an unsigned integer.