------- Comment #3 from edwintorok at gmail dot com 2008-11-07 17:14 ------- (In reply to comment #2) > Subject: Re: New: 'warning: comparison between signed and > unsigned' shouldn't be given for equality comparisons > > On Fri, 7 Nov 2008, edwintorok at gmail dot com wrote: > > > Consider this code: > > > > int foo(int a, unsigned b) > > { > > return a != b; > > } > > > $ gcc -O2 -Wall -W foo.c -c > > foo.c: In function foo: > > foo.c:3: warning: comparison between signed and unsigned integer expressions > > Seems correct to me. You could have a == -1 and b == UINT_MAX and they > would compare equal, but it's not at all obvious from the code that this > is the user's intent; and if it is their intent, they can use an explicit > cast.
It would be nice to have a separate warning option for ==, != comparisons, and another for <,>,<=,>= comparisons. The problem is that with code that uses read(2) and write(2) you get lots of warnings, and adding casts is time-consuming and of little benefit. I'd like to turn -Wsign-compare into -Werror for 'make distcheck' in my project, but can't do that due to the != comparisons. If I would have a distinct -W flag for it, I could -Wno-error that. > > > FWIW, if I change int to 'short int' or 'signed char' (and unsigned > > appropriately) I get no warning. > > Both short and unsigned short will be promoted to int before the > comparison, so you won't get -1 and USHRT_MAX comparing equal. > Ok -- http://gcc.gnu.org/bugzilla/show_bug.cgi?id=38046