检测字符串在C中是双精度还是长整数 [英] Detecting if a string is a double or long in C

查看:79
本文介绍了检测字符串在C中是双精度还是长整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理的char []包含应表示双精度数值或长整数的文本.
我需要编写一个函数来检测表示的是上述哪种数据类型(如果有的话).

I'm dealing with an char[] containing text which should represent a double number value OR a long number.
I'm in a need to write a function that detects which of the above data-types is represented (if any).

我考虑过使用strtol()并检查它是否无法解析整个字符串,如果失败,则使用strtod().

I thought about using strtol() and check if it fails to parse the entire string, and if it fails, using strtod().

我很高兴看到是否有更好的选择. 谢谢.

I would be happy to see whether there is a better option to do so. Thanks.

推荐答案

我考虑过使用strtol()并检查它是否无法解析整个字符串,如果失败,则使用strtod().

I thought about using strtol() and check if it fails to parse the entire string, and if it fails, using strtod().

我认为这是个好主意,但我认为没有更好的主意.通常,实现自己的解析例程是一个坏主意.

I think that's a good idea and I don't think there is a better one. Implementing your own parsing routine is generally a bad idea.

我会在调用strtol之前从尾随空白处修剪字符串,以免出现假阴性.

I would trim the string from trailing whitespace before calling strtol to avoid false negatives.

这篇关于检测字符串在C中是双精度还是长整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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