【发布时间】:2010-11-08 13:47:49
【问题描述】:
以下代码:
#include <stdint.h>
int main() {
uint8_t Byte;
if (Byte < 0) { }
}
发出以下编译警告:
main.cpp: In function `int main()':
main.cpp:6: warning: comparison is always false due to limited range of data type
这很好。但是当我将条件更改为:
(1) if (true || (Byte < 0)) { }
我仍然收到警告,而我希望收到类似“比较始终为真......”的警告:)
如果我将字节声明更改为:
(2) uint32_t Byte;
警告消失。
我该如何解释这种行为?
我的系统是 RHEL 5.3 64 位,附带 gcc 4.1.2。
编辑:
(1) 不是问题,我只是误解了编译器警告。 它并没有说整个 if 是 false 而是“Byte
所以问题只有 (2) - 为什么 Byte 类型会触发编译器警告。 常量“0”的类型是 int,所以它有 4 个字节。所以它必须与 uint8_t 与 int
的比较有关【问题讨论】: