Pádraig Brady <[EMAIL PROTECTED]> writes:

> I don't see why gcc is giving this warning, as
> there is no comparison between signed and unsigned here.
> For example in the following program compiled
> with -Wsign-compare why does the second assignment
> give a warning, while the first doesn't?
>
> #include <stdio.h>
> #include <limits.h>
>
> int main(void)
> {
>     int i=0;
>     unsigned u=UINT_MAX;
>
>     if (u)  i = u; /* no warning */
>     i = u ? u : i; /* warning with -Wsign-compare */

GCC is warning about the difference in signedness between the two
alternatives of the conditional operator.  That can lead to subtle bugs,
since the expressions needs to be converted to a common type, the
signedness of which can change depending of the involved types.  The
first case is just an assignment, where the right operand is always
converted to the left operand, thus no surprising behaviour (but
-Wsign-conversion still warns for this case).

Andreas.

-- 
Andreas Schwab, SuSE Labs, [EMAIL PROTECTED]
SuSE Linux Products GmbH, Maxfeldstraße 5, 90409 Nürnberg, Germany
PGP key fingerprint = 58CA 54C7 6D53 942B 1756  01D3 44D5 214B 8276 4ED5
"And now for something completely different."


_______________________________________________
Bug-coreutils mailing list
Bug-coreutils@gnu.org
http://lists.gnu.org/mailman/listinfo/bug-coreutils

Reply via email to