c - Using ssize_t vs int -
code
i've got function can write in one of 4 possible ways:
int do_or_die(int retval); int do_or_die(ssize_t retval); ssize_t do_or_die(int t retval); ssize_t do_or_die(ssize_t retval);
and called both of these ways library functions:
written = do_or_die(write(...)); // posix write returns ssize_t printed = do_or_die(printf(...)); // printf returns int
questions
- which prototype should use?
- what types should give
written
,printed
?
i want have robust , standard code, while still having 1 do_or_die
function.
i using c99 in case, if answer different c11, i'd know too, future.
there's no guarantee in c or posix standards sizeof(int) >= sizeof(ssize_t)
, nor other way around. typically ssize_t
larger int
, safe , portable option in c99 use intmax_t
instead argument , return value.
the guarantees have wrt. relationship between int
, ssize_t
are:
int
can store values of @ least range [-2^15 ... 2^15-1] per iso cssize_t
can store values of @ least range [-1 ... 2^15-1] per posix (see_posix_ssize_max
).
(interestingly, there isn't guarantee ssize_t
can store negative counterparts of positive range. it's not signed size_t
, "size type" error value.)
Comments
Post a Comment