比较int和无符号整型 [英] Compare int and unsigned int
本文介绍了比较int和无符号整型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果一个人需要比较 INT X
与 unsigned int类型是
这是更安全/好/更好的 C99
和 GCC 4.4 +
:
If one needs to compare int x
with unsigned int y
which is safer/better/nicer in C99
and with gcc 4.4+
:
-
(无符号整数)x ==是
-
X ==(INT)Y
(unsigned int)x == y
x == (int)y
有什么关系?
推荐答案
最安全的是检查的数量范围内铸造前:
Safest is to check that the number is in range before casting:
if (x >= 0 && ((unsigned int)x) == y)
这篇关于比较int和无符号整型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文