为什么比较一个无符号的Int> = 0 a“无意义的比较”? [英] Why Is Comparing if an Unsigned Int >= 0 a "Pointless Comparison"?

查看:610
本文介绍了为什么比较一个无符号的Int> = 0 a“无意义的比较”?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




Pe186无符号无符号比较零




当我试图编译下面的代码时:

  for(clLoop = cpLoopStart ; clLoop> = 0; clLoop--)
{
//做某事
}

我不明白为什么。我可以理解,如果我正在寻找小于零的值,因为 unsigned int 永远不能为负数。但是我在这里找的是如果等于为零,肯定可以是 unsigned int



我甚至可以看到这个错误,如果在这个循环中我试图预递减而不是后递减,但是情况并非如此。

> = )零。这个表达式总是成立的,因为无符号整数永远不会小于零。



编译器试图提醒你,你正在编程一个无限循环。 p>

I got warning:

Pe186 "Pointless comparison of unsigned int with zero"

when I tried to compile the following code:

for(clLoop = cpLoopStart; clLoop >= 0; clLoop--)                                  
{
    //Do something
}

I don't understand why. I could understand, if I were looking for a value less than zero, since an unsigned int can never be negative. But all I am looking for here is if it is equal to zero, which an unsigned int certainly can be.

I could even see this error if in this loop I tried to pre-decrement instead of post-decrement, but again that is not the case.

解决方案

You check whether the unsigned int is greater than or equal (>=) zero. This expression will always be true, because unsigned integers will never be less than zero.

The compiler tries to warn you that you are about to program an infinite loop.

这篇关于为什么比较一个无符号的Int> = 0 a“无意义的比较”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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