What is the difference between "int" and "signed int" and why "int" can hold values over it limits? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

What is the difference between "int" and "signed int" and why "int" can hold values over it limits?

Hey guys, what is the difference between "int" and "signed int" if they both can hold the same values no matter the polarity?  example: int a = -3 is the same as signed int = -3 why don't we use directly "int" rather than "signed int" what is the utility of this last one? Second question: They say that "int" can hold numbers from -32768 to 32767 but I've tried a big number such as: 987654321 in the IDE and it compiled it without any Errors!! what does this means?

19th Dec 2016, 8:39 PM
Fahem
Fahem - avatar
1 Answer
+ 2
int is actually exactly the same as signed int, and is just shorthand for it since we use it more commonly than unsigned int. (signed) int is different to unsigned int in that unsigned int cannot hold negative values. Also, the size of (signed /unsigned) int is platform specific. Nowadays on most 32bit platforms it is 32bits so well, which is why your program was quite happy with that big number. Your -32768 to 32767 is actually the range for (signed) short. Signed or unsigned short is always 16bits, so the 2 ranges are: Signed short : Pos: 0 -> 32767 (0 -> 7FFFh) Neg: -32768 -> -1 (8000h -> FFFFh) Unsigned short: 0 -> 65535 (0 -> FFFFh) Both signed and unsigned has the same number of numbers in their range (both being 16bits), which is given by 2^16. They just cover different ranges. Negative numbers is actually coded on something called "2s complement", which is why -1 == FFFFh.
19th Dec 2016, 10:29 PM
Ettienne Gilbert
Ettienne Gilbert - avatar