开发者

Guarantees on sizeof(int) in Single UNIX or POSIX

What are the size guarantees开发者_如何学JAVA on int in Single UNIX or POSIX? This sure is a FAQ, but I can't find the answer...


With icecrime's answer, and a bit further searching on my side, I got a complete picture:

ANSI C and C99 both mandate that INT_MAX be at least +32767 (i.e. 2^15-1). POSIX doesn't go beyong that. Single Unix v1 has the same guarantee, while Single Unix v2 states that the minimum acceptable value is 2 147 483 647 (i.e. 2^31-1).


The C99 standard specifies the content of header <limits.h> in the following way :

Their implementation-defined values shall be equal or greater in magnitude (absolute value) to those shown, with the same sign.

  • 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

There are no size requirements expressed on the int type.

However, the <stdint.h> header offer the additional exact-width integer types int8_t, int16_t, int32_t, int64_t and their unsigned counterpart :

The typedef name intN_t designates a signed integer type with width N, no padding bits, and a two’s complement representation. Thus, int8_t denotes a signed integer type with a width of exactly 8 bits.


POSIX doesn't cover that. The ISO C standard guarantees that types will be able to handle at least a certain range of values but not that they'll be of a particular size.

The <stdint.h> header introduced with C99 will get you access to types like int16_t that do.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜