c - “由于数据类型范围有限,比较总是正确的”C 中的警告?

标签 c gcc cell warnings

我有以下代码

//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/

相关文章:

excel - 使用 C# 在 Excel 工作表中查找并删除一些文本

ios - JTAppleCalendar 预选当前日期而不触发 isSelected

c - 读取输入引脚的状态并显示在 LED 上 - LPC1115

c - 将变量参数传递给 makecontext 函数

c++ - 段错误。地址 0x0 未被堆栈、malloc 或(最近)释放

c++ - 如何在 MINGW 中使用 __uuidof?

C 钩子(Hook)/回调系统

连接 : connection refused in c program at client side

c - 如何从特定目录调用 system()?

java - 尝试为 JTable 中的特定单元格着色... getTableCellRendererComponent Override