|
-
November 24th, 2004, 01:40 PM
#1
difference between int and long ?
Hy there,
can someone help me with this?
what are the possible value ranges of the following types:
int
short int
long int
?
when i read this:
http://msdn.microsoft.com/library/de...ype_ranges.asp
correctly,
int and long are the same (both are 32 bit and can hold values from –2,147,483,648 to 2,147,483,647)
so what is the difference between int and long then?
-
November 24th, 2004, 02:13 PM
#2
Re: difference between int and long ?
Short Answer
int is a platform specific variable sized number.. long is platform independent constant sized number. It is true that on 32 bit windows variable sized int is the same size as constant sized long. 32 bits..
Longer Answer
Back in the day of the 16 bit OS. int changed sizes based on your memory model.
16 bits for small and medium models I believe, and I think 32 for large memory model. When OSII/Windows NT embraced a 32 bit standard memory models went away and int became a 32 bit number. Likely on 64 bit linux box int will be a 64 bit number... The representation of int changes based upon the compiler settings ( memory models if they exist ) and the base <a href="http://www.serverlogic3.com/lm/rtl2.asp?k=operating%20system" onmouseover="window.status='operating system'; return true;" onmouseout="window.status=''; return true;">operating system</a>....
Long is by definition two words or four bytes. or 32 bits.. It will never change.
The difference is only important if you change OS's or your code does. Or of course you have to deal with intels legacy segmented memory schemes.. Which thank god have gone the way of the dodo in 32 bit land..
Last edited by JMS; November 24th, 2004 at 02:16 PM.
-
November 24th, 2004, 02:41 PM
#3
Re: difference between int and long ?
>> what are the possible value ranges of the following types:
>> int
>> short int
>> long int
The value for short int and long int are 16 bit and 32 bit respectively...
Typically there is a .h file called range.h I believe which will tell you the ranges of all the variables which are platform specfic for any compiler.. gnu vc++ etc...
I think ranges.h is it... It might be something else, you can grep for it....
-
November 24th, 2004, 02:48 PM
#4
Re: difference between int and long ?
 Originally Posted by JMS
Short Answer
int is a platform specific variable sized number.. long is platform independent constant sized number. <snip>
Long is by definition two words or four bytes. or 32 bits.. It will never change.
No. The standard doesn't say that long is 32 bits or 4 bytes or something like that. What the standard says is
 Originally Posted by C++ Standard
There are four signed integer types: “signed char”, “short int”, “int”, and “long int.” In this
list, each type provides at least as much storage as those preceding it in the list. Plain ints have the natural
size suggested by the architecture of the execution environment39) ; the other signed integer types are
provided to meet special needs.
There is a paragraph about unsigned integer types, also.
-
November 24th, 2004, 02:52 PM
#5
Re: difference between int and long ?
 Originally Posted by JMS
Long is by definition two words or four bytes. or 32 bits..
Small correction.... long is by definition at least 32 bits.
It's possible that it could be more. Some processors have 9-bit bytes, and it is likely that longs will occupy 36 bits on these systems. It's also possible that a compiler may define short int as 16 bits, int as 32 bits, long as 64 bits, and long long as 128 bits.
Kevin Hall
-
November 24th, 2004, 02:57 PM
#6
Re: difference between int and long ?
And in response to Gabriel's post, Gabriel is correct; the standard doesn't acutally define the number of bits defining any integer data type. However the standard does (I believe it is in a different section from what Gabriel quoted), the minimum range of integer data types. One can infer from this the minimum number of bits needed.
Kevin Hall
-
November 25th, 2004, 04:14 AM
#7
Re: difference between int and long ?
The standard says that sizeof char<=short<=int<=long.
-
November 26th, 2004, 06:28 PM
#8
Re: difference between int and long ?
Kernighan&Ritchie -- page 34.
"int - an integer, typically reflecting the natural size of integers on the host machine.
In addition, there are a number of qualifiers which can be applied to int's : short, long, and unsigned. Short and long refer to different sizes of integers."
-
November 27th, 2004, 06:13 AM
#9
Re: difference between int and long ?
 Originally Posted by JMS
Kernighan&Ritchie -- page 34.
"int - an integer, typically reflecting the natural size of integers on the host machine.
In addition, there are a number of qualifiers which can be applied to int's : short, long, and unsigned. Short and long refer to different sizes of integers."
That is C. It is, however, still correct in the context of C++.
On a broader level, note that neither C nor C++ go as far as assuming that a byte has a certain number of bits (8). Hardware with diffrent byte width is rare, but not unknown.
It is a mistake many people make: they take one implementations specification as being generally applicable. The truth is exactly the other way around: the standard does not specify a width for primitive data types, rendering them implementation defined. In other words, the implementation needs to specify them.
-
November 28th, 2004, 03:29 PM
#10
Re: difference between int and long ?
 Originally Posted by Robert Rostek
what are the possible value ranges of the following types:
int
short int
long int
One strategy is to define your own integer types, like SHORT, INT and LONG to be for example 16, 32 and 64 bits respectively. Then you plug in whatever types a specific compiler requires to give you those sizes.
-
November 30th, 2004, 01:26 AM
#11
Re: difference between int and long ?
 Originally Posted by _uj
One strategy is to define your own integer types, like SHORT, INT and LONG to be for example 16, 32 and 64 bits respectively. Then you plug in whatever types a specific compiler requires to give you those sizes.
C99 helps out in this area. It defines the following data types in <stdint.h>:
Data types at least n-bits wide
int_least8_t
uint_least8_t
int_least16_t
uint_least16_t
int_least32_t
uint_least32_t
int_least64_t
uint_least64_t
Data types at least n-bits wide, but the fastest implementation available (often these will just be native machine word size if possible)
int_fast8_t
uint_fast8_t
int_fast16_t
uint_fast16_t
int_fast32_t
uint_fast32_t
int_fast64_t
uint_fast64_t
Data types exactly n-bits wide (note that the C99 standard says that these types do not have to be supported by compilers. It is more of a recomendation for semi-portable code)
int8_t
uint8_t
int16_t
uint16_t
int32_t
uint32_t
int64_t
uint64_t
That's really all. Given that many compilers will support C data types in C++ (GNU compiler for example), then these data types may be available your C++ compiler. Unfortunately, Microsoft doesn't support C99.
Kevin Hall
-
November 30th, 2004, 02:54 AM
#12
Re: difference between int and long ?
Reading the book <<c++ programming language>>
I come from China and I have read this book .
classic!
-
December 4th, 2004, 08:04 AM
#13
Re: difference between int and long ?
thanks for all the answers!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|