我有以下代码
//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;
}
当我编译这个 (ppu-gcc 4.1.1) 时,我收到以下警告
warning: comparison is always true due to limited range of data type
即使 char 的范围是 -127 到 127,WIDTH 是 8,这在 char 的范围内。我已经尝试过将 WIDTH 显式转换为 char,但仍然出现错误。
最佳答案
您确定 char
已签名吗?尝试将字段显式声明为 signed char
并查看结果。
关于c - “由于数据类型范围有限,比较总是正确的”C 中的警告?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/757482/