Originally posted by andreano
View Post
https://wiki.sei.cmu.edu/confluence/...nversion+rules
The rank of any extended signed integer type relative to another extended signed integer type with the same precision is implementation-defined but still subject to the other rules for determining the integer conversion rank.
The rules for - values to a unsigned Int are really horrible in the C standard if you compiler obeys them. That right all signed values get converted to that in the Noncompliant Code Example of that page as well. Yes that example there has two int of equal size and different signedness and how the standard says for it to screw up. Of course it does not have to screw up that way due to rank of unsigned vs signed being implementation defined.
The reality if you are in C yes it defined as I will be compiler implementation detail how it works.
-Wsign-compare is warning you when you are doing things that are in the defined C standard as noncompliant to C standard code and if that code does something you are not expecting that your problem. So -Wsign-compare errors are absolute ones you should not ignore..
Code:
int si = -1; unsigned int ui = 1; printf("%d\n", si < ui);
It all depends on the conversion rank of int vs unsigned int. Please note the compiler is free to put these in any order they like by the C standard.
So if int(the signed one) is a high conversion rank than unsigned int then the unsigned value gets converted to the int(the signed one) here resulting this sample code is of course true(1). Now if the conversion rank is the other way over -1 in the int end up converted to unsgined end up a UINT_MAX so that compare is now false(0). This is valid for the C standard to have both these outcomes.
Code:
int si = -1; signed int ssi = -2; printf("%d\n", si > ssi);
Horrible this is all perfectly valid to the c standard. These areas of horrible defined undefined are really nasty.
Comparing signed to unsigned in C is dangerous and just as dangerous as comparing signed to signed of different types even if they are the same bit width all due to the C standard defines of ranks of conversion. The type conversion rules of C are a total stack of evil that can result in very wacky things happening and be perfectly valid to standard. -Wsign-compare is truly warning you about non standard conforming(not safe) C code.
Comment