Long and Int

This is a discussion on Long and Int within the C++ Programming forums, part of the General Programming Boards category; I'm very new to C++ and was curious why long and int are the same size (both are 4 bytes/32 ...

  1. #1
    gin
    gin is offline
    Hmm...? gin's Avatar
    Join Date
    Jun 2008
    Location
    Glasgow, Scotland
    Posts
    51

    Long and Int

    I'm very new to C++ and was curious why long and int are the same size (both are 4 bytes/32 bits) on my computer. But according to a data type table an int should be 16 bits and a long 32 bits. Why is this so?

    In this case, why would I even consider using a long if they are both the same?

    Thank you.

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,546
    Data types size vary depending on platform and compiler. That is to say, on some systems, long is bigger than int, but on x86 platforms, they are typically the same size.
    Although, if you are looking for a specific size of a type, then look no further than stdint.h and its (u)intxx_t typedefs.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,636
    But according to a data type table an int should be 16 bits and a long 32 bits. Why is this so?
    That table probably gives the minimum sizes (though these are formally given as numeric limits, not number of bits). Other than the minimum limits, the other requirement is that sizeof(int) <= sizeof(long), and having int and long both 32 bits satisfies the requirements.

    In this case, why would I even consider using a long if they are both the same?
    long may be 64 bit on some platforms, and int may be 16 bit on other platforms.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    gin
    gin is offline
    Hmm...? gin's Avatar
    Join Date
    Jun 2008
    Location
    Glasgow, Scotland
    Posts
    51
    Oh, I see. Thank you for clearing that up. So on x86 platfoms there is no point in using the long data type?

    EDIT: Damn, that's some fast replies...

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,546
    It may or may not be.
    For pure size, there's no real reason, but try to match types. If a function returns long, then use a long data type, for example.
    This makes it more portable.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    gin
    gin is offline
    Hmm...? gin's Avatar
    Join Date
    Jun 2008
    Location
    Glasgow, Scotland
    Posts
    51
    Thanks very much. I always like to make sure I get something. Again, thanks for the fast and informative replies laserlight and Elysia.

  7. #7
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,189
    Formally, long is 32 bits, int is the native register size, which on intel processors is 32 bits. Technicalyl on a 64 bit machine an int is 64 bits, although it is usually treated as 32 bits there as well.
    Until you can build a working general purpose reprogrammable computer out of basic components from radio shack, you are not fit to call yourself a programmer in my presence. This is cwhizard, signing off.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,636
    Formally, long is 32 bits
    That is not true. The requirement is just that long have a minimum range of [-2147483647, 2147483647], and that sizeof(int) <= sizeof(long). Consequently, long is at least 32 bits.

    int is the native register size, which on intel processors is 32 bits. Technicalyl on a 64 bit machine an int is 64 bits, although it is usually treated as 32 bits there as well.
    Yes, the C++ Standard does state that "plain ints have the natural size suggested by the architecture of the execution environment39".
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by abachler View Post
    Formally, long is 32 bits, int is the native register size, which on intel processors is 32 bits. Technicalyl on a 64 bit machine an int is 64 bits, although it is usually treated as 32 bits there as well.
    In fact, Windows compiler for x64 doesn't even make long 64-bits, but rather uses their own type (__int64) for 64-bit integers.

    I don't think it's correct to say that "int is the native register size", but rather that it's a size that is generally efficient on the machin and big enough to hold a few tens of thousands or more. Which so happens to be 32 bit on x86/x64. To extend int to 64-bit would for 99% of all applications just cause a waste of space because the integers are now taking up 8 bytes instead of 4. Making "long" 64-bit would actually make sense, but MS has been using LONG for so many of their internal types that it didn't make a good choice for the API to change all over the place. And of course, redefining the API would break a number of existing applications. So they took the choice of making the API stay the same, and make long 32 bits.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,189
    Well, teh ansi standard states that long is specificlly larger than short, and that int is the native reigister size.
    Until you can build a working general purpose reprogrammable computer out of basic components from radio shack, you are not fit to call yourself a programmer in my presence. This is cwhizard, signing off.

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,636
    Well, teh ansi standard states that long is specificlly larger than short, and that int is the native reigister size.
    No, it states that long is specifically not shorter than short, and it does not quite say that int is the native register size, though it does hint along that direction:
    Quote Originally Posted by Paragraph 2, Section 3.9.1, C++ Standard (2003)
    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 environment; the other signed integer types are provided to meet special needs.
    The above references a footnote concerning plain ints:
    that is, large enough to contain any value in the range of INT_MIN and INT_MAX, as defined in the header <climits>.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  2. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  3. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM
  4. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 04:26 PM
  5. A Simple (?) Problem
    By Unregistered in forum C++ Programming
    Replies: 8
    Last Post: 10-12-2001, 04:28 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21