Is the size of C “int” 2 bytes or 4 bytes?

后端 未结 13 2696
不知归路
不知归路 2020-11-22 10:52

Does an Integer variable in C occupy 2 bytes or 4 bytes? What are the factors that it depends on?

Most of the textbooks say integer variables occupy 2 bytes. But whe

13条回答
  •  攒了一身酷
    2020-11-22 10:53

    C99 N1256 standard draft

    http://www.open-std.org/JTC1/SC22/WG14/www/docs/n1256.pdf

    The size of int and all other integer types are implementation defined, C99 only specifies:

    • minimum size guarantees
    • relative sizes between the types

    5.2.4.2.1 "Sizes of integer types " 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

提交回复
热议问题