“比较总是正确的,由于数据类型范围限制,”在C警告? [英] 'comparison is always true due to limited range of data type' warning in C?

查看:241
本文介绍了“比较总是正确的,由于数据类型范围限制,”在C警告?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的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;
}

当我编译这个(PPU-GCC 4.1.1),我得到以下警告

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

即使炭的范围是-127到127和宽度为8位,这是很好一个char的范围内。我已经试过宽度的显式转换为char,但仍然得到了错误。

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.

推荐答案

您确定字符签署?尝试显式声明字段符号字符,看看你会得到什么。

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

这篇关于“比较总是正确的,由于数据类型范围限制,”在C警告?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆