Why Is Comparing if an Unsigned Int >= 0 a “Pointless Comparison”?

后端 未结 10 1743
情话喂你
情话喂你 2021-02-12 10:44

I got warning:

Pe186 \"Pointless comparison of unsigned int with zero\"

when I tried to compile the following code:



        
10条回答
  •  梦如初夏
    2021-02-12 10:58

    As already stated by others unsigned int is always >= 0 i.e. pointless comparison. Mayank Jindal posted for me the starting point for a solution.

    To document the "fix" as a whole: do the following instead, this works as intended:

    unsigned int cpLoopStart = 23U;
    unsigned int clLoop = 0U;
    for(clLoop = cpLoopStart; clLoop > 0U; clLoop--}
    {
        //Do something
    }
    

    clLoop will start at 23 and end at 1 then exits the loop. I hope it was intended that clLoop will never be 0 inside the loop.

    I also prefer to append "U" for unsigned typed values, this makes is clearer also when comparing values and variables.

提交回复
热议问题