4

I am trying to compare an unsigned int with a signed char like this:

int main(){
  unsigned int x = 9;
  signed char y = -1;
  x < y ? printf("s") : printf("g");
  return 0;
}

I was expecting the o/p to be "g". Instead, its "s". What kind of conversion is done here?

4

4 Answers 4

23

Section 6.3.1.8, Usual arithmetic conversions, of C99 details implicit integer conversions.

If both operands have the same type, then no further conversion is needed.

That doesn't count since they're different types.

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.

That doesn't count since one is signed, the other unsigned.

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.

Bingo. x has a higher rank than y so y is promoted to unsigned int. That means that it morphs from -1 into UINT_MAX, substantially larger than 9.

The rest of the rules don't apply since we have found our match but I'll include them for completeness:

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.

Otherwise, both operands are converted to the unsigned integer type corresponding to the type of the operand with signed integer type.


The ranks relevant to this question are shown below. All ranks are detailed in C99, section 6.3.1.1, Boolean, character, and integers so you can refer to that for further details.

The rank of long long int shall be greater than the rank of long int, which shall be greater than the rank of int, which shall be greater than the rank of short int, which shall be greater than the rank of signed char.

The rank of char shall equal the rank of signed char and unsigned char.

4
  • Excellent explanation. I'll add that, it's probably best in such scenarios, to always cast. Pick the type you want the comparison to be done in, and cast both to that type: (signed int)x < (signed int)y would guarantee the results you were originally expecting.
    – Lee
    Feb 23, 2011 at 7:32
  • 4
    Actually, I almost never use unsigned integers. If int can't hold a big enough value, I'll switch up to long or even long long if need be. Too many times I've been bitten by unsigned integers that refuse to go negative, making many of my loops infinite, and leading to angst and much gnashing of teeth :-)
    – paxdiablo
    Feb 23, 2011 at 7:36
  • 3
    @paxdiablo, I just do the inverse and never try to use signed integer types. The correct type for addressing, sizes and stuff like that is size_t which is unsigned. Most of the time this type exactly captures the semantics that I want from an integer type. int is almost always the wrong type for what people are doing with it. The signed type that makes sense for me is ptrdiff_t, but that's it. Feb 23, 2011 at 7:52
  • 2
    Without bothering to look it up, I think that integer promotions are applied before arithmetic conversions. So everything you say is true but note that as far as the arithmetic conversions are concerned, the RHS operand has type int by the time they see it. Feb 23, 2011 at 9:13
2

My guess is y is promoted to unsigned int which becomes a big value (due to wrapping). Hence the condition is satisfied.

4
  • 1
    And if you don't know, don't answer.
    – Jim Balter
    Feb 23, 2011 at 9:36
  • @Jim Balter: I was just trying to give an hint to the OP. If you don't think its a correct hint, feel free to down vote.
    – Asha
    Feb 23, 2011 at 9:41
  • If giving a hint, say "Here's a hint" ... and add it as a comment to the question, not as an answer. Hints and guesses aren't answers.
    – Jim Balter
    Feb 23, 2011 at 9:50
  • Thanks for the suggestion..will take care of that in future comment/answers.
    – Asha
    Feb 23, 2011 at 9:51
2

Ran the following code:

int main(){
  unsigned int x = 9;
  signed char y = -1;
  printf("%u\n", (unsigned int)y);
  x < (unsigned int)y ? printf("s") : printf("g");
  return 0;
}

The output is:

4294967295
s

After a casting, y takes a very large value. That is why output is s.

0
1

The char is promoted to unsigned int, with a value of MAX_UINT, which is greater than 9.

3
  • If it was an unsigned char instead, would the signed char get promoted to unsigned char?
    – badmaash
    Feb 23, 2011 at 7:22
  • @Abhi: yes. Clause 3 (see my answer) still kicks in with "if the operand that has unsigned integer type has rank greater or equal to ...".
    – paxdiablo
    Feb 23, 2011 at 7:47
  • 1
    @Abhi: No, they would both be promoted to int provided that you're not on some weird implementation where sizeof(int) == sizeof(char). In that case, both would end up as unsigned int. So (unsigned char)9 < (signed char)-1 is (loosely speaking) false if sizeof(int) > sizeof(char) and true if sizeof(int) == sizeof(char). I say "loosely" because what matters is the range rather than the size, but assuming no padding bits in int they correspond. It's stupid, but in general don't ever do arithmetic (including comparisons) with types smaller than int in fully portable code. Feb 23, 2011 at 9:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.