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

采用交叉编译的时候,报了这个错误,原来直接在Fedora下面编译没有问题。

原因在于编译器默认char的类型为unsigned类型,和0进行比较时总是true。

这个错误发生在stdsoap2.c文件中(104行的宏定义)

找个一个网上的代码加以说明:

I have the following code

 

  1. //Point.h
  2. #define WIDTH 8
  3. #define HEIGHT 8
  4. typedefstruct Point
  5. {
  6. char x;
  7. char y;
  8. } Point;
  9. //Board.c
  10. #include <stdbool.h>
  11. // Some other functions that we don't care about...
  12. bool inBounds(Point * p)
  13. {
  14. return p->x >= 0
  15. && p->x <= WIDTH
  16. && p->y >= 0
  17. && p->y <= HEIGHT;
  18. }
//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.

我找出了几个有用的回答:

The C and C++ standards allows the character type char to be signed or unsigned, depending on the platform and compiler.Most systems, including x86 GNU/Linux and Microsoft Windows, use signed char,but those based on PowerPC and ARM processors typically use unsigned char.(29) This can lead to unexpected results when porting programs between platforms which have different defaults for the type of char.

 

 

The char type may be signed or unsigned. It depends on your compiler vendor's choice.There might even be a compiler option available. Evidently,char is unsigned for you, so it's always greater than or equal to zero, and thus the compiler warns you.

You're using char here to represent "a numeric type that takes up minimal memory." In that case, I recommend explicitly usingsigned char orunsigned char. (Each is distinct from plainchar, despitechar having to be either signed or unsigned.) Reservechar for when you're holding character data. For numeric data, use one of the other two types.

 

源地址在这里

 

 

 

聚拓互联(http://www.ejutuo.com).Net平台至强虚拟主机供应商,是领先的互联网基础应用服务提供商,主要面向全球客户提供域名注册、国内、香港/美国虚拟主机、企业邮箱、智能建站、虚拟服务器(VPS)、服务器租用、服务器托管等丰富的网络产品服务。

聚拓互联的快速发展与其企业文化密不可分,易网人秉持“团结互助、敬业负责、恪守信誉、积极进取、勇于创新”的企业文化,汇聚了行业内的大量专业人士,拥有多位国内顶尖的linux/freeBSD/unix经验的系统工程师、微软认证工程师和网络安全技术人才。核心团队均为该行业从业多年的专业人士,拥有丰富行业经验和较高威望,并不断改革创新,以满足客户多元化需求为己任,不断进取。 同时,易网坚守 “专业品质、服务为本、诚信经营、恪守信誉”的核心价值观,为客户提供多样、安全、稳定、放心的产品。

posted on 2012-08-23 22:20  天外飞仙2  阅读(2405)  评论(0)    收藏  举报

导航