[Top][All Lists]

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

Re: [avr-gcc-list] Warning message

From: Dave N6NZ
Subject: Re: [avr-gcc-list] Warning message
Date: Tue, 02 Oct 2007 23:25:30 -0700
User-agent: Thunderbird 1.5 (X11/20051201)

Royce Pereira wrote:
Hi all,

What does the following warning mean?

warning: comparison is always true due to limited range of data type

Here is the context:
I have a AVR I'm trying to connect to a DS1307 RTC chip via TWI.

To check the integrity of the DS1307 internal ram, I plan to write 4 bytes, 
0x55,0xaa,0x55,0xaa in the 1st 4 loations, & read them back & verify, whenever 
I power up.

My code contains this:

#define CHK_1307        0x55

uint8 twi_read(void)
        return TWDR;
uint8_t init_1307(void)
       uint8_t ok = 1 ;

      if(twi_read() != CHK_1307) ok= 0;
      if(twi_read() != ~CHK_1307) ok= 0;
//      ^^^^^^
//        The offending line.
I believe what is going on is the C standard requires promotion to int, and that bites (bytes?) you :)

The result of twi_read() and also the ~CHK_1307 get promoted to ints before comparison. So... 0x00xx from twi_read() is compared against -xFFAA, which is clearly always true.

Check the generated code. Casting to uint8_t in the comparison may get you what you want, but check the code, don't take my word for it.


        return ok;

reply via email to

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