C/C++ use of int or unsigned int

后端 未结 6 1311
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-01 09:17

In a lot of code examples, source code, libraries etc. I see the use of int when as far as I can see, an unsigned int would make much more sense.

One pl

6条回答
  •  南笙
    南笙 (楼主)
    2021-01-01 10:13

    It's generally laziness or lack of understanding.

    I aways use unsigned int when the value should not be negative. That also serves the documentation purpose of specifying what the correct values should be.

    IMHO, the assertion that it is safer to use "int" than "unsigned int" is simply wrong and a bad programming practice.

    If you have used Ada or Pascal you'd be accustomed to using the even safer practice of specifying specific ranges for values (e.g., an integer that can only be 1, 2, 3, 4, 5).

提交回复
热议问题