C语言 为什么stdlib.h 中没有strtoi?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6181432/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 08:48:21  来源:igfitidea点击:

Why is there no strtoi in stdlib.h?

cstd

提问by Eli

I have grown accustomed to strtodand variants. I am wondering why there is no strtoi shipped with stdlib.h. Why is it that the integer is left out of this party?

我已经习惯了strtod和变种。我想知道为什么 stdlib.h 没有附带 strtoi。为什么整数被排除在这个党之外?

Specifically I am asking why there is not a version of atoi with the safety features of strtod.

具体来说,我在问为什么没有具有 strtod 安全功能的 atoi 版本。

采纳答案by Wiz

strtol()converts a string to an integer, a long integer but an integer nevertheless. 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.

strtol()将字符串转换为整数,长整数但仍然是整数。有atoi()但在大多数情况下应该避免,因为它缺乏从无效输入报告错误的机制。

回答by chux - Reinstate Monica

Why is there no strtoi in stdlib.h?

为什么stdlib.h 中没有strtoi?

No critical need.

没有关键需求。

In early C, there was not a standard signed type wider than longand all narrower conversions, like int, could be made from strtol()- as done below.

在早期的 C 中,没有一个标准的有符号类型比 更宽long,所有更窄的转换,比如int,都可以从strtol()- 进行 - 如下所示。

IMO, these and their unsignedcounterparts 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, longand inthave 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.

在许多系统上,long并且int具有相同的范围,因此减少了对单独strtoi(). 也满足atoi()了对快速和脏代码的需求,但可能缺乏错误检测。也没有strto_short()norstrto_signchar()等。

It is fairly easy to create a substitute strtoi(). Simplifications exist.

创建替代品相当容易strtoi()。存在简化。

#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);
}

回答by James McNellis

The integer isn't left out of the party: there is strtol, which converts a string to a long, which is an integer type.

整数并没有被排除在外:有strtol将字符串转换long为整数类型的 a 。

回答by Marc K.

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;

回答by sarnold

Don't overlook the SEE ALSOsection of your manpages :)

不要忽略SEE ALSO联机帮助页的部分:)

SEE ALSO
       atof(3), atoi(3), atol(3), strtol(3), strtoul(3)
SEE ALSO
       atof(3), atoi(3), atol(3), strtol(3), strtoul(3)

You're looking for atoi(3). :)

您正在寻找atoi(3). :)

回答by mtrw

It's call atoi. See also Wikipediafor details, including its successor strol.

这是电话atoi。另请参阅Wikipedia以了解详细信息,包括其继任者strol