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?

badmaash
  • 4,775
  • 7
  • 46
  • 61

4 Answers4

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.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • 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 '11 at 07: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 '11 at 07: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. – Jens Gustedt Feb 23 '11 at 07: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. – Steve Jessop Feb 23 '11 at 09:13
2

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

Asha
  • 11,002
  • 6
  • 44
  • 66
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.

Shamim Hafiz - MSFT
  • 21,454
  • 43
  • 116
  • 176
1

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

BeeOnRope
  • 60,350
  • 16
  • 207
  • 386
  • If it was an unsigned char instead, would the signed char get promoted to unsigned char? – badmaash Feb 23 '11 at 07: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 '11 at 07: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. – Steve Jessop Feb 23 '11 at 09:34