Definition of 'int' on different platforms.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dissectcode
    New Member
    • Jul 2008
    • 66

    Definition of 'int' on different platforms.

    I know on some operating systems, an int is the same as a short, while on others, an int is the same as a long. It depends on the word size of the operating system.

    Does anybody know what the definition of int in C is for linux, windows, cygwin etc?
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    You have related int size to operating system, however it is actually related to platform. The platform is mainly dependent on the processor used in the system, although other bits of hardware and how the hardware is put together can also be relevant, however it isn't unusual for these OS to also play a part.

    On most PCs for most 32 and 64 bit versions of the operating systems you mention int is 4 bytes (New versions of Windows, Windows pre Win95 had 16 bit ints). That does not mean that int will necessarily be that size on all platforms using those operating systems, particularly embedded versions running on non-intel platforms.

    Comment

    • gpraghuram
      Recognized Expert Top Contributor
      • Mar 2007
      • 1275

      #3
      I think now all 32 bit compilers the size of int is 32 bit.

      Raghu

      Comment

      • donbock
        Recognized Expert Top Contributor
        • Mar 2008
        • 2427

        #4
        The Standard requires that "int" be no smaller than "short" and no bigger than "long". It is allowed to be any size in between. Implementations are encouraged to select a size for "int" that is the most efficient for the platform. Notice that different compiler vendors for the same platform might disagree on what that most efficient size is.

        The answer to your question can be found in <limits.h>. Macros can be found there that specify the minimum and maximum values supported by "int", "short", and "long".

        You might be able to get your answers merely by examining <limits.h>; however, your compiler might have many levels of nested headers or conditional compilation options to make it hard to interpret. You could write a simple program that prints out all of the relevant limits.

        Comment

        • Banfa
          Recognized Expert Expert
          • Feb 2006
          • 9067

          #5
          In general the C standard requires that

          sizeof(char) <= sizeof(short) <= sizeof(int) <= sizeof(long)

          C++ also makes this requirement but additionally requires that

          short and int are least 16 bits in size and long is at least 32 bits.

          This allows you to make some assumptions in C++ that you can't make in C (which actually makes writing completely portable code easier).

          Comment

          • JosAH
            Recognized Expert MVP
            • Mar 2007
            • 11453

            #6
            C does the same, i.e. the limits.h file specifies the minimal maximum int values and the maximal minimum int values of several sizes (short, normal and long). In C an int takes at least 2 octets and a long takes at least 4 octets.

            kind regards,

            Jos

            Comment

            Working...