我怎么知道atoi函数调用是否成功? [英] how can i Know whether atoi function call succeed?

查看:457
本文介绍了我怎么知道atoi函数调用是否成功?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

自函数atof,atoi,_atoi64,atol返回值为


返回值


每个函数通过将输入字符解释为数字来返回生成的double,int,__ int64或long值

。如果输入

无法转换为值,则返回值为0

(对于atoi和_atoi64),0L(对于atol)或0.0(对于atof)那种类型。如果溢出,返回值为

未定义。


如何判断这些函数调用是否失败或成功?

谢谢。


Baumann @Pan

解决方案

Anonymousgoogledeja写道:

大家好,

自函数atof,atoi,_atoi64,atol返回值是

返回值

每个函数返回double通过将输入字符解释为数字来生成,int,__ int64或long值。如果输入
无法转换为该类型的值,则返回值为0
(对于atoi和_atoi64),0L(对于atol)或0.0(对于atof)。如果溢出,返回值是未定义的。

如何判断这些函数调用是否失败或成功?




_atoi64不是标准功能,但对于其余部分,你不能以b $ b b的形式告诉他们是成功还是失败。某些系统可能会在

故障时设置错误,但这不是必需的。这是使用strto *

功能的一个很好的理由。


Robert Gamble


2005年8月21日19:55:47 -0700,Anonymousgoogledeja

< as ******* @ hotmail.com>在comp.lang.c中写道:


太棒了,你实际上打印了部分文档,但你错过了

最重要的一点!
< blockquote class =post_quotes>大家好,

因为函数atof,atoi,_atoi64,atol返回值是

返回值

每个函数通过将输入字符解释为数字来返回生成的double,int,__ int64或long值。如果输入
无法转换为该类型的值,则返回值为0
(对于atoi和_atoi64),0L(对于atol)或0.0(对于atof)。返回值是
^^^^^^^^^^^^^^^^^^^在溢出的情况下未定义。
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^如何判断这些函数调用是否失败或成功?




实际上你引用的文字并不完全正确。如果您调用其中一个

ato ...函数且转换后的值超出目标类型的范围,则行为未定义,而不仅仅是返回

值。这意味着,根据C标准,可能会发生任何常见的和/或$
未定义行为的令人讨厌的症状:重新格式化你的

硬盘,导致恶魔飞出你的鼻子,导致你的
操作系统向用户显示讽刺消息,并且

终止你的程序......可能性列表是无穷无尽的。


使用ato ...函数的最佳方法是从程序中完全省略它们

。除非你预先检查字符串以确认

结果将在范围内,在这种情况下你也可以同时进行

转换。相反,使用更安全的strto ...函数prototyped< stdlib.h>

无论输入是什么样的,都定义了行为,所以

,因为它不是空指针。如果你传递一个有效的第二个指针

值,你甚至可以判断结果为0是由输入

0引起的,还是没有任何结果转换。


在这里查看示例代码:

http://www.jk-technology.com/c/code/strtol.html


-

Jack Klein

主页: http ://JK-Technology.Com

常见问题解答

comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html

comp.lang.c ++ http://www.parashift。 com / c ++ - faq-lite /

alt.comp.lang.learn.c-c ++
htt p://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html




Robert Gamble写道:

Anonymousgoogledeja写道:

大家好,

因为函数atof,atoi,_atoi64,atol返回的值是
每个函数通过将输入字符解释为数字来返回生成的double,int,__ int64或long值。如果输入
无法转换为该类型的值,则返回值为0
(对于atoi和_atoi64),0L(对于atol)或0.0(对于atof)。如果溢出,返回值是未定义的。

如何判断这些函数调用是否失败或成功?



_atoi64不是标准功能,但对于其余部分,您无法轻易地判断它们是成功还是失败。某些系统可能会在故障时设置错误,但这不是必需的。这是使用strto *
函数的一个很好的理由。

Robert Gamble



什么是下溢?我可以弄清楚溢出是什么。


strtoul的返回值怎么样(''0'',0,10)?我怎么知道

返回值0是转换后的还是因为

转换时出错。


谢谢


Hi all,
since the function atof, atoi, _atoi64, atol returned value are

Return Values

Each function returns the double, int, __int64 or long value produced
by interpreting the input characters as a number. The return value is 0
(for atoi and _atoi64), 0L (for atol), or 0.0 (for atof) if the input
cannot be converted to a value of that type. The return value is
undefined in case of overflow.

how can I tell if these function calls fail or succeed?
thanks.

Baumann@Pan

解决方案

Anonymousgoogledeja wrote:

Hi all,
since the function atof, atoi, _atoi64, atol returned value are

Return Values

Each function returns the double, int, __int64 or long value produced
by interpreting the input characters as a number. The return value is 0
(for atoi and _atoi64), 0L (for atol), or 0.0 (for atof) if the input
cannot be converted to a value of that type. The return value is
undefined in case of overflow.

how can I tell if these function calls fail or succeed?



_atoi64 is not a standard function but for the rest, you cannot
portably tell if they succeed or fail. Some systems may set errno on
failure but this is not required. This is a good reason to use strto*
functions instead.

Robert Gamble


On 21 Aug 2005 19:55:47 -0700, "Anonymousgoogledeja"
<as*******@hotmail.com> wrote in comp.lang.c:

Amazing, you actually printed part of the documentation yet you missed
the most important point!

Hi all,
since the function atof, atoi, _atoi64, atol returned value are

Return Values

Each function returns the double, int, __int64 or long value produced
by interpreting the input characters as a number. The return value is 0
(for atoi and _atoi64), 0L (for atol), or 0.0 (for atof) if the input
cannot be converted to a value of that type. The return value is ^^^^^^^^^^^^^^^^^^^ undefined in case of overflow. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ how can I tell if these function calls fail or succeed?



Actually the text you quoted it not quite correct. If you call one of
the ato... functions and the converted value is outside the range of
the destination type, the behavior is undefined, not just the return
value. That means, according to the C standard, any of the usual and
nasty symptoms of undefined behavior can happen: reformatting your
hard drive, causing demons to fly out of your nose, causing your
operating system to display a sarcastic message to the user and
terminate your program... The list of possibilities is endless.

The best way to use the ato... functions is to completely omit them
from your program. Unless you pre-check the string to verify that the
result will be in range, in which case you might as well do the
conversion at the same time.

Instead, use the much safer strto... functions prototyped <stdlib.h>
which have defined behavior no matter what the input looks like, so
long as it is not a null pointer. If you pass a valid second pointer
value, you can even tell if a result of 0 is caused by an input of
"0", or by not having anything to convert.

Look at the sample code here:

http://www.jk-technology.com/c/code/strtol.html

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html



Robert Gamble wrote:

Anonymousgoogledeja wrote:

Hi all,
since the function atof, atoi, _atoi64, atol returned value are

Return Values

Each function returns the double, int, __int64 or long value produced
by interpreting the input characters as a number. The return value is 0
(for atoi and _atoi64), 0L (for atol), or 0.0 (for atof) if the input
cannot be converted to a value of that type. The return value is
undefined in case of overflow.

how can I tell if these function calls fail or succeed?



_atoi64 is not a standard function but for the rest, you cannot
portably tell if they succeed or fail. Some systems may set errno on
failure but this is not required. This is a good reason to use strto*
functions instead.

Robert Gamble


what''s underflow? i can figure out what overflow is.

what about the return value of strtoul(''0'',0,10)? how can i know if the
returned value 0 is the converted or because there is an error while
converting.

thanks


这篇关于我怎么知道atoi函数调用是否成功?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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