Thread: what are the differences between int, short and long in terms of c standards?

  1. #1
    Registered User
    Join Date
    Jul 2022
    Posts
    17

    what are the differences between int, short and long in terms of c standards?

    I am trying to understand what are the differences between int, short and long in terms of c standards? I'm sure these three are different meaning.


    I would be glad if someone explains the difference in terms of the C standard, not in the terms of the specific compiler

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    The standard requires that the minimum range of these types should be as follows.
    — minimum value for an object of type short int
    SHRT_MIN -32767 // −(215 − 1)
    — maximum value for an object of type short int
    SHRT_MAX +32767 // 215 − 1
    — maximum value for an object of type unsigned short int
    USHRT_MAX 65535 // 216 − 1
    — minimum value for an object of type int
    INT_MIN -32767 // −(215 − 1)
    — maximum value for an object of type int
    INT_MAX +32767 // 215 − 1
    — maximum value for an object of type unsigned int
    UINT_MAX 65535 // 216 − 1
    — minimum value for an object of type long int
    LONG_MIN -2147483647 // −(231 − 1)
    — maximum value for an object of type long int
    LONG_MAX +2147483647 // 231 − 1

    These constants (and others) are found in limits.h

    The only difference between them is the range of values they can store.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Dec 2017
    Posts
    1,626
    It should also be mentioned that
    sizeof(short) <= sizeof(int) <= sizeof(long) <= sizeof(long long)
    A little inaccuracy saves tons of explanation. - H.H. Munro

  4. #4
    Registered User
    Join Date
    Jul 2022
    Posts
    17
    @salem
    As per your advice there is no difference between int and short they both serve the same purpose. It becomes very difficult to understand that in C standard two different words are written but the purpose is same, there is no difference in them.

  5. #5
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    945
    Quote Originally Posted by john.c View Post
    It should also be mentioned that
    sizeof(short) <= sizeof(int) <= sizeof(long) <= sizeof(long long)
    Which means that all integer types could be 64 bits wide (e.g., on an esoteric system that can handle only 64-bit values) and it would still be C standard conforming.

    But most modern systems (x86, x64, Arm, Arm64, etc) follow the 16/32/32/64 or 16/32/64/64 integer size conventions.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > As per your advice there is no difference between int and short they both serve the same purpose.
    No.

    These are the minimum capability required by the standard.

    On any real machine, especially 32bit and 64bit machines, int's are usually 32 bit or occasionally 64 bit.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Feb 2022
    Posts
    45
    One of the things to understand is that the standard C types are defined in a way which allows implementation on many different instruction set architectures, not all of which are necessarily byte-oriented (though realistically, there are few if any modern CPUs which aren't).

    Furthermore, even with byte-oriented ISAs, the 'natural' word size can vary between ISAs (or even different operating modes on the same ISA) - most older systems had default word sizes of 16 bits or even 8 bits, and larger types had to be supported in software. Conversely, some 64-bit ISAs might not readily work with smaller types.

    As has already been said, for modern ISAs and compilers, the sizes generally end up defined as

    short - 16 bits
    int - 32 bits
    long - 32 or 64 bits
    long long - 64 bits

    Thus, the standard defines ranges in which the types could fit. A short could be any size equal to or more than 8 bits, and any size less than or equal to 16 bits, for example.

    The general assumption is that for most programming purposes, you don't need exact sizes, just relative ones.

    Now, in the C99 standard and later, there exist an additional set of types in the <stdint.h> header which define signed and unsigned types specifying exact bit sizes in the form [u]intn_t, partly as a reflection of the near-universal adoption of byte-oriented ISAs and 32/64 bit natural types. If you need to use a known fixed size, these are the ones to use.
    Last edited by Schol-R-LEA-2; 08-03-2022 at 03:07 PM.

  8. #8
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    945
    Quote Originally Posted by Schol-R-LEA-2 View Post
    Thus, the standard defines ranges in which the types could fit. A short could be any size equal to or more than 8 bits, and any size less than or equal to 16 bits, for example.
    A short must be at least 16 bits wide, per the minimum range that Salem graciously posted for us (the range allows for 1's complement which "wastes" one bit pattern on negative zero (though whether it's a waste depends on who you ask)).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 06-26-2015, 09:06 AM
  2. Replies: 11
    Last Post: 02-04-2012, 09:46 AM
  3. How come short + int = int but short + uint = long?
    By y99q in forum C# Programming
    Replies: 2
    Last Post: 10-29-2011, 11:16 AM
  4. short and long int
    By gogo in forum C Programming
    Replies: 3
    Last Post: 03-10-2003, 06:25 AM
  5. bytes long and short
    By SAMSAM in forum Game Programming
    Replies: 0
    Last Post: 03-08-2003, 09:50 AM

Tags for this Thread