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 thanuint
orunsigned 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)