比较int和无符号整型 [英] Compare int and unsigned int

查看:157
本文介绍了比较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+:


  1. (无符号整数)x ==是

  2. X ==(INT)Y

  1. (unsigned int)x == y
  2. x == (int)y

有什么关系?

推荐答案

最安全的是检查的数量范围内铸造前:

Safest is to check that the number is in range before casting:

if (x >= 0 && ((unsigned int)x) == y)

这篇关于比较int和无符号整型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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