Click to See Complete Forum and Search --> : difference between int and long ?
Robert Rostek
November 24th, 2004, 12:40 PM
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/default.asp?url=/library/en-us/vclang/html/_langref_data_type_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?
JMS
November 24th, 2004, 01:13 PM
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..
JMS
November 24th, 2004, 01:41 PM
>> 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....
Gabriel Fleseriu
November 24th, 2004, 01:48 PM
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 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.
KevinHall
November 24th, 2004, 01:52 PM
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.
KevinHall
November 24th, 2004, 01:57 PM
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.
cilu
November 25th, 2004, 03:14 AM
The standard says that sizeof char<=short<=int<=long.
JMS
November 26th, 2004, 05:28 PM
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."
Gabriel Fleseriu
November 27th, 2004, 05:13 AM
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.
_uj
November 28th, 2004, 02:29 PM
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.
KevinHall
November 30th, 2004, 12:26 AM
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. :(
existsluawjb
November 30th, 2004, 01:54 AM
Reading the book <<c++ programming language>>
I come from China and I have read this book .
classic!
Robert Rostek
December 4th, 2004, 07:04 AM
thanks for all the answers!
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.