是什么将字符串转换为整数的sscanf或与atoi之间的区别? [英] What is the difference between sscanf or atoi to convert a string to an integer?

查看:153
本文介绍了是什么将字符串转换为整数的sscanf或与atoi之间的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

GCC 4.4.4 C89

gcc 4.4.4 c89

什么是更好的一个字符串转换为一个整数值。

What is better to convert a string to an integer value.

我曾尝试2种不同方法的atoi和sscanf。预期双方的工作。

I have tried 2 different methods atoi and sscanf. Both work as expected.

char digits[3] = "34";
int device_num = 0;

if(sscanf(digits, "%d", &device_num) == EOF) {
    fprintf(stderr, "WARNING: Incorrect value for device\n");
    return FALSE;
}

或使用的atoi

device_num = atoi(digits);

我在想,在sscanf的效果会更好,你可以检查错误。然而,与atoi没有做任何检查。

I was thinking that the sscanf would be better as you can check for errors. However, atoi doesn't doing any checking.

推荐答案

您有3种选择:


  • 的atoi :这可能是最快的,如果你在性能关键code。使用它,但它确实没有错误报告。如果字符串不是以一个整数开头,它将返回0。如果字符串包含整数后的垃圾,它会转换初始部分而忽略其他。如果数量太大,不适合在 INT ,其行为是不确定的。

  • 的sscanf :有些错误报告,你有很大的灵活性什么类型来存储字符/短路/ INT /长/长长的(符号/无符号版本/为size_t / ptrdiff_t的/ intmax_t型)。返回值是成功转换为%D的数量,所以扫描将返回0,如果字符串不以整数开头。您可以使用%D%N来存储在另一个变量读取整数之后的第一个字符的索引,从而检查是否整个字符串被转换或者,如果有垃圾之后。然而,像的atoi ,整数溢出的行为是不确定的。

  • 与strtol 和家庭:强大的错误报告,只要你进行调用之前设置错误号 0。返回值溢出和错误号将被设置。您可以选择2的基数为36,或指定0为基数,以自动间preT领先 0X 0 分别为十六进制和八进制。类型选择转换为签署长/长长/的将intmax_t /无符号的版本。如果你需要一个较小的类型,你可结果总是存储在临时无符号长变量,并检查溢出你自己。由于这些功能需要一个指向指针参数,你也可以得到一个指向第一个字符以下转换的整数,是免费的,这样你就可以判断整个字符串是一个整数,或者如果需要在字符串中解析后续的数据。

  • atoi: This is probably the fastest if you're using it in performance-critical code, but it does no error reporting. If the string does not begin with an integer, it will return 0. If the string contains junk after the integer, it will convert the initial part and ignore the rest. If the number is too big to fit in int, the behavior is unspecified.
  • sscanf: Some error reporting, and you have a lot of flexibility for what type to store (signed/unsigned versions of char/short/int/long/long long/size_t/ptrdiff_t/intmax_t). The return value is the number of conversions that succeed, so scanning for "%d" will return 0 if the string does not begin with an integer. You can use "%d%n" to store the index of the first character after the integer that's read in another variable, and thereby check to see if the entire string was converted or if there's junk afterwards. However, like atoi, behavior on integer overflow is unspecified.
  • strtol and family: Robust error reporting, provided you set errno to 0 before making the call. Return values are specified on overflow and errno will be set. You can choose any number base from 2 to 36, or specify 0 as the base to auto-interpret leading 0x and 0 as hex and octal, respectively. Choices of type to convert to are signed/unsigned versions of long/long long/intmax_t. If you need a smaller type you can always store the result in a temporary long or unsigned long variable and check for overflow yourself. Since these functions take a pointer to pointer argument, you also get a pointer to the first character following the converted integer, for free, so you can tell if the entire string was an integer or parse subsequent data in the string if needed.

就个人而言,我会建议与strtol 家人的的目的。如果你正在做一些快速和肮脏的,可能与atoi满足您的需求。

Personally, I would recommend the strtol family for most purposes. If you're doing something quick-and-dirty, atoi might meet your needs.

顺便说一句,有时我觉得我需要解析的地方前导空格,符号等,不应该被接受的数字。在这种情况下,它是pretty该死容易推出自己的循环(为(X = 0;(签名)* S-'0'小于10; S ++)X = 10 * X +( * S-'0'); ),也可以使用如果(ISDIGIT(* S))×=与strtol(S,放大器; S,10);否则/ * *错误/ 的稳健性。

As an aside, sometimes I find I need to parse numbers where leading whitespace, sign, etc. are not supposed to be accepted. In this case it's pretty damn easy to roll your own for loop (for (x=0; (unsigned)*s-'0'<10; s++) x=10*x+(*s-'0');) Or you can use if (isdigit(*s)) x=strtol(s, &s, 10); else /* error */ for robustness.

这篇关于是什么将字符串转换为整数的sscanf或与atoi之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆