[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [PATCH] avoid -Wsign-compare warnings

From: Andreas Schwab
Subject: Re: [PATCH] avoid -Wsign-compare warnings
Date: Fri, 11 Jul 2008 14:03:29 +0200
User-agent: Gnus/5.110009 (No Gnus v0.9) Emacs/22.2 (gnu/linux)

Pádraig Brady <address@hidden> 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 Schwab, SuSE Labs, address@hidden
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."

reply via email to

[Prev in Thread] Current Thread [Next in Thread]