Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'comparison is always true due to limited range of data type' warning in C?

Tags:

c

gcc

warnings

cell

I have the following code

//Point.h
#define WIDTH 8
#define HEIGHT 8

typedef struct Point
{
  char x;
  char y;
} Point;

//Board.c
#include <stdbool.h>

// Some other functions that we don't care about... 

bool inBounds(Point * p)
{
  return p->x >= 0
    && p->x <= WIDTH
    && p->y >= 0
    && p->y <= HEIGHT;
}

When I compile this (ppu-gcc 4.1.1), I get the following warning

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

even though the range of char is -127 to 127 and WIDTH is 8, which is well inside the range of a char. I've already tried an explicit cast of WIDTH to a char, but still got the error.

like image 845
Paul Wicks Avatar asked Apr 16 '09 18:04

Paul Wicks


1 Answers

Are you sure that char is signed? Try declaring the fields explictly as signed char and see what you get.

like image 84
Dan Breslau Avatar answered Oct 25 '22 10:10

Dan Breslau