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? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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

6th Jul 2016, 12:40 AM
Jastria Rahmat
Jastria Rahmat - avatar
2 Answers
+ 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.
6th Jul 2016, 12:55 AM
Garme Kain
Garme Kain - avatar
0
Unsigned can be used to declare integers that should NEVER be negative to avoid errors.
6th Jul 2016, 6:12 PM
Christian Roos
Christian Roos - avatar