Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between unsigned long/long/int in c/c++?

Tags:

It seems all of them take 4 bytes of space,

so what's the difference?

like image 305
user198729 Avatar asked Mar 31 '10 03:03

user198729


People also ask

What is the difference between unsigned long and unsigned long long?

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).

What are the difference between long int and unsigned int?

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.

What is unsigned long long int in C?

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.

What is the difference between long int and long long int in C?

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 .


1 Answers

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."

like image 145
rlbond Avatar answered Oct 25 '22 03:10

rlbond