+ 1
just asking. in C++, why do we need to define signed & unsigned integer? why don't we always use signed? it's better. we can freely assign plus and minus. so what's the catch of using unsigned?
please explain as easy as possible. analogies are preferred. thank you
2 Antworten
+ 5
If you want to store a values greater or equal than 2^31 but smaller than 2^32 you need to use at least unsigned. Also, if you don't need to store negative values unsigned is the choice.
For example, variable "life", wich counts the life of something. Negative values do not make sense and you dont want to waste half of the memory an int gives you to use.
0
Unsigned can be used to declare integers that should NEVER be negative to avoid errors.