I have grown accustomed to strtod
and variants.
I am wondering why there is no strtoi shipped with stdlib.h.
Why is it that the integer is left out of this part
Why is there no strtoi in stdlib.h?
No critical need.
In early C, there was not a standard signed type wider than long
and all narrower conversions, like int
, could be made from strtol()
- as done below.
IMO, these and their unsigned
counterparts are now missing C functions and a design shortcoming in the current standard C library (C17/18).
On many systems, long
and int
have the same range and so there is a reduced need for a separate strtoi()
. Also the atoi()
fills the need for quick and dirty code, but can lack error detection. There also is not a strto_short()
nor strto_signchar()
, etc.
It is fairly easy to create a substitute strtoi()
. Simplifications exist.
#include
#include
#include
static long strto_subrange(const char *s, char **endptr, int base,
long min, long max) {
long y = strtol(s, endptr, base);
if (y > max) {
errno = ERANGE;
return max;
}
if (y < min) {
errno = ERANGE;
return min;
}
return y;
}
// OP's goal
int strtoi(const char *s, char **endptr, int base) {
#if INT_MAX == LONG_MAX && INT_MIN == LONG_MIN
return (int) strtol(s, endptr, base);
#else
return (int) strto_subrange(s, endptr, base, INT_MIN, INT_MAX);
#endif
}
short strtoshort(const char *s, char **endptr, int base) {
return (short) strto_subrange(s, endptr, base, SHRT_MIN, SHRT_MAX);
}
signed char strtoschar(const char *s, char **endptr, int base) {
return (signed char) strto_subrange(s, endptr, base, SCHAR_MIN, SCHAR_MAX);
}
#include
int16_t strtoint16(const char *s, char **endptr, int base) {
return (int16_t) strto_subrange(s, endptr, base, INT16_MIN, INT16_MAX);
}