[英]atoi function in C not working properly
Can somebody explain why atoi function don't work with nuber who have more than 9 digits. 有人可以解释为什么atoi函数不能与超过9位的nuber一起工作。
For example: 例如:
I entered 123456789, program says 123456789, but when i entered 12345678901 program say -519403114... Thanks for helping. 我输入123456789,程序说123456789,但是当我输入12345678901程序时说-519403114 ...感谢您的帮助。
int main ()
{
int i;
char szinput [256];
printf ("Enter a Card Number:");
fgets(szinput,256,stdin);
i=atoi(szinput);
printf("%d\n",i);
getch();
return 0;
}
Don't use atoi()
, or any of the atoi*()
functions, if you care about error handling. 如果您关心错误处理,请不要使用atoi()
或任何atoi*()
函数。 These functions provide no way of detecting errors; 这些功能无法检测错误; neither atoi(99999999999999999999)
nor atoi("foo")
has any way to tell you that there was a problem. atoi(99999999999999999999)
和atoi("foo")
都没有办法告诉你有问题。 (I think that one or both of those cases actually has undefined behavior, but I'd have to check to be sure.) (我认为这些案例中的一个或两个实际上都有未定义的行为,但我必须检查以确定。)
The strto*()
functions are a little tricky to use, but they can reliably tell you whether a string represents a valid number, whether it's in range, and what its value is. strto*()
函数使用起来有点棘手,但它们可以可靠地告诉您字符串是否表示有效数字,是否在范围内,以及它的值是什么。 (You have to deal with errno
to get full checking.) (你必须处理errno
以获得全面检查。)
If you just want an int value, you can use strtol()
(which, after error checking, gives you a long
result) and convert it to int
after also checking that the result is in the representable range of int
(see INT_MIN
and INT_MAX
in <limits.h>
). 如果你只是想要一个int值,你可以使用strtol()
其中,错误检查后,给你一个long
的结果),并将其转换为int
也检查的结果是在可表现范围后, int
(见INT_MIN
和INT_MAX
在<limits.h>
)。 strtoul()
gives you an unsigned long
result. strtoul()
给你一个unsigned long
结果。 strtoll()
and strtoull
() are for long long
and unsigned long long
respectively; strtoll()
和strtoull
()分别为long long
和unsigned long long
; they're new in C99, and your
compiler
implementation might not support them (though most non-Microsoft implementations probably do). 它们是C99中的新功能,并且您的
编译器
实现可能不支持它们(尽管大多数非Microsoft实现可能都支持它们)。
Because you are overflowing an int
with such a large value. 因为你溢出了一个如此大的值的int
。
Moreover, atoi
is deprecated and thread-unsafe on many platforms, so you'd better ditch it in favour of strto(l|ll|ul|ull)
. 此外, atoi
在许多平台上都被弃用并且不安全,所以你最好strto(l|ll|ul|ull)
它而转向strto(l|ll|ul|ull)
。
Consider using strtoull
instead. 考虑使用strtoull
代替。 Since unsigned long long
is a 64-bit type on most modern platforms, you'll be able to convert a number as big as 2 ^ 64
( 18446744073709551616
). 由于unsigned long long
在大多数现代平台上都是64位类型,因此您可以将数字转换为2 ^ 64
( 18446744073709551616
)。
To print an unsigned long long
, use the %llu
format specifier. 要打印unsigned long long
,请使用%llu
格式说明符。
if you are writing win32 application then you can use windows implementation of atoi, for details check the below page. 如果你正在编写win32应用程序,那么你可以使用atoi的windows实现,详情请查看下面的页面。
http://msdn.microsoft.com/en-us/library/czcad93k%28v=vs.80%29.aspx http://msdn.microsoft.com/en-us/library/czcad93k%28v=vs.80%29.aspx
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.