本文介绍了我怎么知道atoi函数调用是否成功?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 大家好, 自函数atof,atoi,_atoi64,atol返回值为 返回值 每个函数通过将输入字符解释为数字来返回生成的double,int,__ int64或long值 。如果输入 无法转换为值,则返回值为0 (对于atoi和_atoi64),0L(对于atol)或0.0(对于atof)那种类型。如果溢出,返回值为 未定义。 如何判断这些函数调用是否失败或成功? 谢谢。 Baumann @Pan 解决方案 _atoi64不是标准功能,但对于其余部分,你不能以b $ b b的形式告诉他们是成功还是失败。某些系统可能会在 故障时设置错误,但这不是必需的。这是使用strto * 功能的一个很好的理由。 Robert Gamble 实际上你引用的文字并不完全正确。如果您调用其中一个 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 _atoi64不是标准功能,但对于其余部分,您无法轻易地判断它们是成功还是失败。某些系统可能会在故障时设置错误,但这不是必需的。这是使用strto * 函数的一个很好的理由。 Robert Gamble 什么是下溢?我可以弄清楚溢出是什么。 strtoul的返回值怎么样(''0'',0,10)?我怎么知道 返回值0是转换后的还是因为 转换时出错。 谢谢 Hi all,since the function atof, atoi, _atoi64, atol returned value areReturn ValuesEach function returns the double, int, __int64 or long value producedby 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 inputcannot be converted to a value of that type. The return value isundefined in case of overflow.how can I tell if these function calls fail or succeed?thanks.Baumann@Pan 解决方案_atoi64 is not a standard function but for the rest, you cannotportably tell if they succeed or fail. Some systems may set errno onfailure but this is not required. This is a good reason to use strto*functions instead.Robert GambleActually the text you quoted it not quite correct. If you call one ofthe ato... functions and the converted value is outside the range ofthe destination type, the behavior is undefined, not just the returnvalue. That means, according to the C standard, any of the usual andnasty symptoms of undefined behavior can happen: reformatting yourhard drive, causing demons to fly out of your nose, causing youroperating system to display a sarcastic message to the user andterminate your program... The list of possibilities is endless.The best way to use the ato... functions is to completely omit themfrom your program. Unless you pre-check the string to verify that theresult will be in range, in which case you might as well do theconversion 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, solong as it is not a null pointer. If you pass a valid second pointervalue, 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 KleinHome: http://JK-Technology.ComFAQs forcomp.lang.c http://www.eskimo.com/~scs/C-faq/top.htmlcomp.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 _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 Gamblewhat''s underflow? i can figure out what overflow is.what about the return value of strtoul(''0'',0,10)? how can i know if thereturned value 0 is the converted or because there is an error whileconverting.thanks 这篇关于我怎么知道atoi函数调用是否成功?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!