10

Often values are known to be positive. For example TCP/UDP sequence number is always positive value. Both int and unsigned int are big enough to store even the biggest sequence number so I can use any of these types. There are many other examples when values are known to be positive.

Are there any reasons to use unsigned type when capacity of regular signed type is enough (and often more than enough)?

Personally I tend to use regular types because:

  • int is probably a little bit more readable than uint or unsigned int
  • I don't need to include extra headers for UINT etc.
  • I will avoid extra casts somewhere further in the program

Reasons to use unsigned type I can imagine:

  • help compiler generated better code?
  • help another programmer to understand that variable is unsigned
  • avoid possible bugs (for example when int is assigned to UINT compiler likely will generate compile-time error and we should check that value we assign is not negative)
Morwenn
  • 21,684
  • 12
  • 93
  • 152
Oleg Vazhnev
  • 23,239
  • 54
  • 171
  • 305
  • Some thoughts about it here: http://embeddedgurus.com/stack-overflow/2009/08/a-tutorial-on-signed-and-unsigned-integers/ – Morwenn Apr 24 '13 at 09:36
  • 1
    thanks, also I've found that question just now http://stackoverflow.com/questions/12225521/should-unsigned-ints-be-used-if-not-necessary?rq=1 – Oleg Vazhnev Apr 24 '13 at 09:38
  • 1
    No. There are some arguments against using `unsigned` - Stroustrup alludes to this in passing in The C++ Programming Language somewhere - Lakos went off and wrote a chapter about it in one of his books: tedious. Example issues: if you have values a and b, `abs(a - b)` tends to work intuitively for ints but not unsigned; and it's noteworthy that Standard conversions can kick in silently so the kind of checks you might hope to get from using unsigned don't work anyway - negative values become silently huge and vice versa. – Tony Delroy Apr 24 '13 at 09:39
  • 1
    Mixing signed and unsigned is an important source of bugs. I would stick to signed in all but specialist situations. – john Apr 24 '13 at 09:40
  • Me too; unsigned types are really nice, but they come with many gotchas, plus messier syntax. – geometrian Apr 24 '13 at 09:44
  • 1
    @john: not mixing them means not using STL at all? – Andriy Tylychko Apr 24 '13 at 09:46
  • Andrew Koenig wrote a nice article about this: http://www.drdobbs.com/cpp/unsigned-arithmetic-useful-but-tricky/240001198 – Jan Herrmann Apr 24 '13 at 10:08
  • @AndyT It's unfortunate that the STL uses unsigned types. – john Apr 24 '13 at 10:09
  • @john: yeah, I mean if you use STL you cannot avoid using unsigned ints, and since you're already mixing ints I think it's less error-prone to use unsigned ints when apropriate. – Andriy Tylychko Apr 24 '13 at 10:50

1 Answers1

1

One reason is that comparing signed and unsigned numbers can lead to surprising results. In C and (I think) C++, comparing signed and unsigned numbers causes the signed number to be interpreted as unsigned. If the signed value happens to be negative, reading it as unsigned will give a LARGER value than any unsigned number, which is not what you want. See this question for an example in Objective-C, which uses the same conversion rules as C.

Community
  • 1
  • 1
Caleb
  • 124,013
  • 19
  • 183
  • 272