CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 13 of 13
  1. #1
    Join Date
    Sep 1999
    Location
    Europe / Austria / Tirol
    Posts
    159

    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?

  2. #2
    Join Date
    May 2000
    Location
    Washington DC, USA
    Posts
    715

    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.

  3. #3
    Join Date
    May 2000
    Location
    Washington DC, USA
    Posts
    715

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

  4. #4
    Join Date
    Jun 2001
    Location
    Switzerland
    Posts
    4,443

    Exclamation Re: difference between int and long ?

    Quote 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
    Quote 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.
    Gabriel, CodeGuru moderator

    Forever trusting who we are
    And nothing else matters
    - Metallica

    Learn about the advantages of std::vector.

  5. #5
    Join Date
    Nov 2002
    Location
    Foggy California
    Posts
    1,245

    Re: difference between int and long ?

    Quote 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

  6. #6
    Join Date
    Nov 2002
    Location
    Foggy California
    Posts
    1,245

    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

  7. #7
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: difference between int and long ?

    The standard says that sizeof char<=short<=int<=long.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  8. #8
    Join Date
    May 2000
    Location
    Washington DC, USA
    Posts
    715

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

  9. #9
    Join Date
    Jun 2001
    Location
    Switzerland
    Posts
    4,443

    Re: difference between int and long ?

    Quote 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.
    Gabriel, CodeGuru moderator

    Forever trusting who we are
    And nothing else matters
    - Metallica

    Learn about the advantages of std::vector.

  10. #10
    Join Date
    Nov 2003
    Posts
    1,405

    Re: difference between int and long ?

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

  11. #11
    Join Date
    Nov 2002
    Location
    Foggy California
    Posts
    1,245

    Re: difference between int and long ?

    Quote 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

  12. #12
    Join Date
    Nov 2004
    Posts
    3

    Re: difference between int and long ?

    Reading the book <<c++ programming language>>
    I come from China and I have read this book .
    classic!

  13. #13
    Join Date
    Sep 1999
    Location
    Europe / Austria / Tirol
    Posts
    159

    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
  •  





Click Here to Expand Forum to Full Width

Featured