What is the difference between “short int” and “int” in C?

后端 未结 9 2011
闹比i
闹比i 2020-12-24 07:22

How is short int (or short) and int different in C? They have the same size and range. If they are essentially the same, what is the use of having two data types?

相关标签:
9条回答
  • 2020-12-24 08:17

    C99 N1256 standard draft

    All that we now for sure is that:

    2 <= sizeof(short) <= sizeof(int)
    

    5.2.4.2.1 Sizes of integer types <limits.h> gives the minimum sizes:

    1 [...] Their implementation-defined values shall be equal or greater in magnitude (absolute value) to those shown [...]

    • UCHAR_MAX 255 // 2 8 − 1
    • USHRT_MAX 65535 // 2 16 − 1
    • UINT_MAX 65535 // 2 16 − 1
    • ULONG_MAX 4294967295 // 2 32 − 1
    • ULLONG_MAX 18446744073709551615 // 2 64 − 1

    6.2.5 Types then says:

    8 For any two integer types with the same signedness and different integer conversion rank (see 6.3.1.1), the range of values of the type with smaller integer conversion rank is a subrange of the values of the other type.

    and 6.3.1.1 Boolean, characters, and integers determines the relative conversion ranks:

    1 Every integer type has an integer conversion rank defined as follows:

    • The rank of long long int shall be greater than the rank of long int, which shall be greater than the rank of int, which shall be greater than the rank of short int, which shall be greater than the rank of signed char.
    • The rank of any unsigned integer type shall equal the rank of the corresponding signed integer type, if any.
    • For all integer types T1, T2, and T3, if T1 has greater rank than T2 and T2 has greater rank than T3, then T1 has greater rank than T3
    0 讨论(0)
  • 2020-12-24 08:20

    It depends on the system. Some OSes won't have the same length for both types.

    0 讨论(0)
  • 2020-12-24 08:22

    short and int must be at least 16 bits, long must be at least 32 bits, and that short is no longer than int, which is no longer than long. Typically, short is 16 bits, long is 32 bits, and int is either 16 or 32 bits.

    0 讨论(0)
提交回复
热议问题