It seems all of them take 4 bytes of space,
so what's the difference?
Unsigned long variables are extended size variables for number storage, and store 32 bits (4 bytes). Unlike standard longs unsigned longs won't store negative numbers, making their range from 0 to 4,294,967,295 (2^32 - 1).
Unsigned int is only guaranteed to be able to hold the numbers between 0 and 65535 (inclusive), while unsigned long int is guaranteed to be able to hold the numbers between 0 and 4 294 967 295.
long int Data Type: unsigned long int data type denotes a 32 – bit integer. It does not use a bit to store the sign. Hence it can hold only positive values between 0 and 4,294,967,295 (2 32 – 1). long is used whenever an int data type is not sufficient to represent a number.
long and long int are identical. So are long long and long long int . In both cases, the int is optional. As to the difference between the two sets, the C++ standard mandates minimum ranges for each, and that long long is at least as wide as long .
First of all, the size of int/long is unspecified. So on your compiler, an int
and a long
might be the same, but this isn't universal across compilers.
As for the difference between unsigned long
and long
:
Assuming 4 bytes, a long
has the range of -2,147,483,648
to 2,147,483,647
. An unsigned long has the range of 0
to 4,294,967,295
.
One other difference is with overflow. For a signed type, an overflow has unspecified behavior. But for an unsigned type, overflow is guaranteed to "wrap around."
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With