[英]Why is there no strtoi in stdlib.h?
I have grown accustomed to strtod
and variants.我已经习惯了
strtod
和变体。 I am wondering why there is no strtoi
shipped with <stdlib.h>
.我想知道为什么
<stdlib.h>
没有附带strtoi
。 Why is it that the integer type is left out of this party?为什么 integer 类型被排除在此方之外?
Specifically I am asking why there is not a version of atoi
with the safety features of strtod
?具体来说,我问为什么没有具有
strtod
安全功能的atoi
版本?
strtol()
converts a string to an integer, a long integer but an integer nevertheless. strtol()
将字符串转换为 integer、长 integer 但仍然是 integer。 There is atoi()
but it should be avoided in most cases due to the fact that it lacks a mechanism for error reporting from invalid input.有
atoi()
但在大多数情况下应该避免使用它,因为它缺乏从无效输入报告错误的机制。
Why is there no strtoi in stdlib.h?
为什么 stdlib.h 中没有 strtoi?
No critical need.没有关键需求。
In early C, there was not a standard signed integer type wider than long
and all narrower conversions, like int
, could be made from strtol()
- as done below.在早期的 C 中,没有标准签名的 integer 类型比
long
更宽,所有更窄的转换,如int
,都可以从strtol()
进行 - 如下所示。
IMO, these and their unsigned
counterparts are now missing C functions and a design shortcoming in the current standard C library (C17/18). IMO,这些及其
unsigned
的对应物现在缺少 C 功能和当前标准 C 库 (C17/18) 中的设计缺陷。
On many systems, long
and int
have the same range and so there is a reduced need for a separate strtoi()
.在许多系统上,
long
和int
具有相同的范围,因此减少了对单独strtoi()
的需求。 atoi()
fills the need for quick and dirty code to convert to an int
, but can lack error detection or cause undefined behavior (UB). atoi()
满足了将快速和脏代码转换为int
的需求,但可能缺乏错误检测或导致未定义行为(UB)。 There also is no strto_short()
nor strto_signchar()
, etc.也没有
strto_short()
或strto_signchar()
等。
It is fairly easy to create a substitute strtoi()
.创建替代
strtoi()
相当容易。 Simplifications exist.存在简化。
#include <errno.h>
#include <limits.h>
#include <stdlib.h>
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 <stdint.h>
int16_t strtoint16(const char *s, char **endptr, int base) {
return (int16_t) strto_subrange(s, endptr, base, INT16_MIN, INT16_MAX);
}
The integer isn't left out of the party: there is strtol
, which converts a string to a long
, which is an integer type. integer 并没有被排除在外:有
strtol
,它将字符串转换为long
,它是 integer 类型。
This is what I have been using.这是我一直在使用的。
long long_val;
int int_value;
errno = 0;
long_val = strtol (theString, NULL, 10);
if (errno)
handle_error;
if ((long) someIntMin > long_val || long_val > (long) someIntMax)
handle_invalid;
int_value = (int) long_val;
Don't overlook the SEE ALSO
section of your manpages:)不要忽略手册页的
SEE ALSO
部分:)
SEE ALSO atof(3), atoi(3), atol(3), strtol(3), strtoul(3)
You're looking for atoi(3)
.您正在寻找
atoi(3)
。 :) :)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.