This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: inapropriate 'comparison is always false due to limited range of data type' warning
- From: Jakub Jelinek <jakub at redhat dot com>
- To: Rodolfo Hansen <kryptt at gmail dot com>
- Cc: gcc at gcc dot gnu dot org
- Date: Fri, 29 Sep 2006 17:34:03 +0200
- Subject: Re: inapropriate 'comparison is always false due to limited range of data type' warning
- References: <142ace2f0609290755l298d73abi5d4789ac1fb94042@mail.gmail.com>
- Reply-to: Jakub Jelinek <jakub at redhat dot com>
On Fri, Sep 29, 2006 at 10:55:27AM -0400, Rodolfo Hansen wrote:
> v857829@vdsdr-125 ~ $ gcc -O0 test.c -o test
> test.c: In function 'main':
> test.c:5: warning: comparison is always false due to limited range of data
> type
>
>
> does not make sense to me in the following code:
>
>
>
> #include <stdio.h>
> int main (char *argv[], int argc) {
> unsigned short int number;
>
> for (number=0; number < ~(number&(~number)) ; number++) {
> printf("hello");
> }
>
> return 0;
> }
This question is appropriate for gcc-help mailing list, not here.
> i believe gcc is misinterpreting ~(number&(~number))
>
> any coments, help welcome.
No, please read the C promotion rules. number&(~number) is always 0,
~0 on two's complement arches is -1 (signed int).
number < ~(number&(~number)) comparison is done in promoted mode - int,
so is (int) number < ~0 which is the same as (int) number < -1. As
no value of unsigned short number promoted to int is negative (unless
sizeof (int) == sizeof (short)), the compiler correctly warns that
the condition is always false.
Jakub