atoi错误 [英] atoi error

查看:138
本文介绍了atoi错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何才能得到正确的结果:

char * str;

str =" 10974000000";

i = atoi(str) ;

printf(" i =",i);

-----输出:


i = 2617245696


---

如果str较小(10974000)那么结果是正确的

解决方案

Rodion写道:

我怎样才能得到正确的结果:
char * str;
str =" 10974000000" ;;


这是一个34位的数字。

i = atoi(str);




atoi大概在你的平台上返回32位。


[我自己用sscanf ..]


Tom


于8/20/2004 6:40 PM Rodion写道:

我如何才能得到正确的结果:
char * str;
str =" 10974000000";
i = atoi(str);
printf(" i =",i);
-----输出:

i = 2617245696

---如果str较小(10974000)那么结果是正确的



你没有宣布我。

在一本好的

我正在跳过很多关于
$的常用长篇大论b $ b未定义的行为。


如果你仔细阅读了大部分

得到有用答案的帖子,你会发现大多数

他们有最小的可编辑的

代码,其中包括illus记录手头的问题。

在这种情况下,它甚至可以解决你的问题。


-

Shanmu。


Rodion< m _ ****** @ mail.ru>写道:

我怎样才能得到正确的结果:
char * str;
str =" 10974000000" ;;
i = atoi(str);
printf(" i =",i);


那将是


printf(" i =%d \ n",i);

或类似的东西,不是吗?

-----输出:
i = 2617245696
---
如果str是较小的(10974000)然后结果是正确的




正如其他人已经在你的系统上指出的那样,数字是

可能太大而无法表示通过int。我强烈地建议您放弃atoi()而不是使用strtol()。

它不仅可以让你将这个数字转换为长数,它还可以

让你有机会通过例如严格的错误检查设置

errno到ERANGE如果您传递的号码无法存储

长(并且它可以让您找出输入字符串的数量

被转换等。)

问候,Jens

-

\ Jens Thoms Toerring ___ 济*********** @ physik.fu-berlin.de

\ __________________________ http://www.toerring.de


How can I get correct result:
char * str;
str = "10974000000";
i = atoi(str);
printf("i=" , i);
----- output:

i=2617245696

---
If str is smaller(10974000) then result is correct

解决方案

Rodion wrote:

How can I get correct result:
char * str;
str = "10974000000";
This is a 34 bit number.
i = atoi(str);



atoi presumably returns 32 bits on your platform.

[i''d use sscanf myself..]

Tom


on 8/20/2004 6:40 PM Rodion Wrote:

How can I get correct result:
char * str;
str = "10974000000";
i = atoi(str);
printf("i=" , i);
----- output:

i=2617245696

---
If str is smaller(10974000) then result is correct


you havenot declared i.
Lookup the syntax of printf() in a good
c reference book.
I am skipping a lot of the usual tirade about
undefined behaviour.

If you go thorough most of the posts which
got useful answers, you will notice that most
of them have the smallest piece of compilable
code which illustrates the problem at hand.
In this case, it might even solve your problem.

--
Shanmu.


Rodion <m_******@mail.ru> wrote:

How can I get correct result:
char * str;
str = "10974000000";
i = atoi(str);
printf("i=" , i);
That would be

printf("i=%d\n" , i);

or something similar, wouldn''t it?
----- output: i=2617245696 ---
If str is smaller(10974000) then result is correct



As someone else already pointed out on your system the number is
probably too large to be represented by an int. I would strongly
recommend that you give up on atoi() and instead use strtol().
Not only will it let you convert that number to a long, it also
gives you a chance to do serious error checking by e.g. setting
errno to ERANGE if the number you passed it could not be stored
in a long (and it lets you find out how much of the input string
was converted etc.).
Regards, Jens
--
\ Jens Thoms Toerring ___ Je***********@physik.fu-berlin.de
\__________________________ http://www.toerring.de


这篇关于atoi错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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