In the following code, assume that an unsigned char is smaller than an int.
int f(void) { int i = -2; unsigned char uc = 1; return (i + uc) < 17; }
The code causes the compiler to issue the following warning when you use the -xtransition option:
line 6: warning: semantics of "<" change in ISO C; use explicit cast
The result of the addition has type int (value preserving) or unsigned int (unsigned preserving), but the bit pattern does not change between these two. On a two’s-complement machine:
i: 111...110 (-2) + uc: 000...001 ( 1) =================== 111...111 (-1 or UINT_MAX)
This bit representation corresponds to -1 for int and UINT_MAX for unsigned int. Thus, if the result has type int, a signed comparison is used and the less-than test is true. If the result has type unsigned int, an unsigned comparison is used and the less-than test is false.
The addition of a cast serves to specify which of the two behaviors is desired:
value preserving: (i + (int)uc) < 17 unsigned preserving: (i + (unsigned int)uc) < 17
Because differing compilers chose different meanings for the same code, this expression can be ambiguous. The addition of a cast is as much to help the reader as it is to eliminate the warning message.
The same situation applies to the promotion of bit-field values. In ISO C, if the number of bits in an int or unsigned int bit-field is less than the number of bits in an int, the promoted type is int; otherwise, the promoted type is unsigned int. In most older C compilers, the promoted type is unsigned int for explicitly unsigned bit-fields, and int otherwise.
Similar use of casts can eliminate situations that are ambiguous.