Using ssize_t vs int

There’s no guarantee in the POSIX standard that sizeof(int) >= sizeof(ssize_t), nor the other way around. Typically ssize_t is larger than int, but the safe and portable option in C99 is to use intmax_t instead for the argument and the return value.

The only guarantees you have wrt. the relationship between int and ssize_t are:

  • int can store values of at least the range [-2^15 … 2^15-1] per ISO C
  • ssize_t can store values of at least the range [-1 … 2^15-1] per POSIX (see _POSIX_SSIZE_MAX).

(Interestingly, there isn’t even a guarantee that ssize_t can store the negative counterparts of its positive range. It’s not a signed size_t, but a “size type” with an error value.)

Leave a Comment