Comparison is always true? No way!

Go To Last Post
5 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The compiler is giving this error:

../dplc_lcd.h:45: warning: comparison is always false due to limited range of data type

I have this code:

#define LCD_CLR_CHAR 0xFF
static int lcd_putchar(char c, FILE *stream)
{
	if(c == LCD_CLR_CHAR)
	{
		lcd_clrscr();
		return 0;
	}
	LCD_PORT = enable_underlight | 0x04 | LCD_BYTE_HIGH_NYBBLE(c);
	LCD_EN_PULSE();
	LCD_PORT = enable_underlight | 0x04 | LCD_BYTE_LOW_NYBBLE(c);
	LCD_EN_PULSE();
	lcd_wait(200);
	return 0;
}

The char has to be char and it 0xFF is well within the given range of 8 bits.

Does the compiler care wheter it is signed/unsigned when i go hex? How to get rid of the error? LCD_CLR_CHAR has to be 0xFF, and the function definition cant have unsigned as i have bound it as the standart output function.

Ofcourse the error stops apearing when I set the LCD_CLR_CHAR to 127, and starts when I set it to 128 and higher. therefore it's a signed/unsigned problem. So how to get rid of this?

There are pointy haired bald people.
Time flies when you have a bad prescaler selected.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Typecast c to an unsigned char before using it in the comparison.

BTW, have you set the -funsigned-char compiler option?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

lfmorrison: Thanks! Works great!

There are pointy haired bald people.
Time flies when you have a bad prescaler selected.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

When not using int8_t, uint8_t I've taken to always specifying signed or unsigned on char just to be sure about this kind of thing - so there's no possibility of mis-interpretation.

Cliff

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
When not using int8_t, uint8_t I've taken to always specifying signed or unsigned on char just to be sure about this kind of thing - so there's no possibility of mis-interpretation.

I do that, but in this case i couldn't! The default IO function is set that way, so when I gave

../dplc_lcd.h:56: warning: initialization from incompatible pointer type

So I figured the first error would be better than the second. :-D But now there are no errors.

There are pointy haired bald people.
Time flies when you have a bad prescaler selected.