Is it important to declare a variable as unsigned if you know it should never be negative? Does it help prevent anything other than negative numbers being fed into a function th
This has value for the same reason that "const
correctness" has value. If you know that a particular value shouldn't change, declare it const
and let the compiler help you. If you know a variable should always be non-negative, then declare it as unsigned
and the compiler will help you catch inconsistencies.
(That, and you can express numbers twice as big if you use unsigned int
rather than int
in this context.)