views:

121

answers:

5

I learned the range of int and long on 32 bit windows is:

signed int: -32767 to 32767
signed long: -2147483647 to 2147483647

why does the int has same range as long type as mentioned on msdn? http://msdn.microsoft.com/en-us/library/s3f49ktz%28VS.80%29.aspx

+1  A: 

exact length is not mandated. What is mandated is that long cannot be smaller than int.

http://www.faqs.org/docs/learnc/x401.html

aaa
A: 

You int would compare to the __int16 whereas your long would compare to __int32.

You have to look at the Bytes column for this to make more sense.

astander
A: 

I assume you meant to write this:

signed int: -2147483647 to 2147483647
...

They are the same for entirely historical reasons. The C++ standard doesn't mandate a particular size for each type, the only guarantee between these two types is that sizeof(int) <= sizeof(long).

Marcelo Cantos
+2  A: 

Your premise is wrong. int and long are both 32 bit on (both) 32 and 64 bit Windows.

Alex
+1: 64-bit Windows uses the LLP64 model, whereas the civilised world uses LP64 on 64-bit Linux et al.
Paul R
@Paul, I don't agree that [LP64 is necessarily better though](http://blogs.msdn.com/oldnewthing/archive/2005/01/31/363790.aspx).
Alex
A: 

When working with binary representations of integers you can calculate the maximum range of signed data types based on the number of bits used to represent the data using the formula:

lowest value: -2^(N-1) highest value: 2^(N-1) - 1

where N is the number of bits.

For one reason or another, the "signed int" type you're talking about is represented by 16 bits, and the "signed long" is represented by 32 bits.

Also notice that the ranges you posted seem to be incomplete, note that the range of a 16 bit signed int goes down to -32768, which is one lower than your posted range.

Mark E