Sizeof (int) guarantees on Single UNIX or POSIX

What are the size guarantees for int on Single UNIX or POSIX? This is necessarily a FAQ, but I can not find the answer ...

+6
unix sizeof posix
source share
3 answers

The C99 standard indicates the contents of the <limits.h> header as follows:

Their values ​​determined by the implementation must 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 // - (2 15 - 1)
  • maximum value for an object of type int
    INT_MAX +32767 // 2 15 - 1
  • maximum value for an object of type unsigned int
    UINT_MAX 65535 // 2 16 - 1

There are no size requirements in int .

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

The name typedef intN_t denotes an integer type with a width of N, no fill bits and a double set representation. Thus, int8_t denotes an integer type with a width of exactly 8 bits .

+3
source share

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

ANSI C and C99 state that INT_MAX is at least +32767 (i.e. 2 ^ 15-1). POSIX does not. Single Unix v1 has the same guarantee, while Single Unix v2 claims that the minimum value is 2,147,483,647 (i.e. 2 ^ 31-1).

+4
source share

POSIX does not cover this. The ISO C standard ensures that types will be able to handle at least a certain range of values, but not that they will have a certain size.

The <stdint.h> header, introduced in C99, will give you access to types such as int16_t .

-one
source share

All Articles