What does the comparison `(int)-1 > (unsigned)0` evaluate to?
0 upvotes
Practice with AISoon
It evaluates to true. When a signed and unsigned integer appear in the same expression, C's implicit conversion rules promote the signed value to unsigned. The value -1 is reinterpreted as UINT_MAX (4294967295 on a 32-bit system), which is far greater than 0.
This is one of the most dangerous C traps in embedded code, where unsigned types are used extensively for registers, counters, and sizes. The defense: never mix signed and unsigned in comparisons without an explicit cast, and enable -Wsign-compare and -Wconversion compiler warnings.
Source: C / C++ Concepts Q&A
