0

Is it appropriate to use unsigned integer for phone number and credit card?

I used unsigned for credit number input and phone number. So, I was confused between integer and unsigned. I googled but unable to figure out. Is it appropriate to use unsigned integer for contact and credit card? If it is then what's the explanation of using? If not then why not?

21st Oct 2020, 10:23 AM
Samia Haque
Samia Haque - avatar
1 Réponse
+ 1
As coder kitten said, in the event of your credit card number starting with 0, you would loose information by saving it as a number (either int or unsigned int) Anyway just to help you understand the difference between int and unsigned int: Let's say you got 2 "spaces" to write a decimal number (that means you can write from 0 to 9 in each of the spaces). If, always using only this two spaces, you want to write also negative numbers, you need to use one of the spaces to store the sign (either + or -) and the other space to store a number from 0 to 9. The greatest number you can write is +9. That's how an int works. If you know that you need to write only positive numbers (like credit card numbers or minutes to write a comment) then using a space to always write the + sign is a waste of resources. You don't need to keep track of the sign, so you can use both the spaces for numbers from 0 to 9. Now the biggest number you can write is 99. That's how unsigned int works. Hope it was clear enough
21st Oct 2020, 1:07 PM
Davide
Davide - avatar