Red Hat Bugzilla – Bug 499467
gcc compiler giving a warning for uint16 < 0 comparison, but not for uint32
Last modified: 2009-05-06 16:01:18 EDT
Description of problem:
While compiling some code, I changed a type from u32 to u16. When I compiled, it gave me a warning that "comparison is always false due to limited range of data type". Then I wondered why it did not complain when the variable was defined as u32.
So, I wrote a small test to see if I can reproduce. I can.
It could also be that I am missing something, but thought I would file just in case.
Version-Release number of selected component (if applicable):
gcc (GCC) 4.1.2 20071124 (Red Hat 4.1.2-42)
typedef unsigned int uint32;
typedef unsigned short uint16;
if (a < 0)
if (b < 0)
Steps to Reproduce:
1. Compile above code
2. Look at compiler message generated at compile time
go.c: In function 'main':
go.c:16: warning: comparison is always false due to limited range of data type
I was expecting to see two warnings.
It is very unclear what compiler do you use, when you file a bug against Fedora 10 and then mention a gcc version that is not shipped there.
Anyway, all of gcc 4.1.x, 4.3.x, 4.4.0 and trunk emit 2 warnings with -W:
q.c:13: warning: comparison of unsigned expression < 0 is always false
q.c:16: warning: comparison is always false due to limited range of data type
The reason the warnings are different is that in C those 2 comparisons are really different, in the first case the comparison is (unsigned int) a < (unsigned int) 0
while in the second case because of integral promotions it is
(int) b < 0.
Sorry about that. Turned out to be a RHEL5.2 box.
Thanks for the explanation.