确定一个C字符串在C有效的int [英] Determine if a C string is a valid int in C

查看:109
本文介绍了确定一个C字符串在C有效的int的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要检查,如果C字符串是一个有效的整数。

I need to check if a C string is a valid integer.

我都尝试

int num=atoi(str);

int res=sscanf(str, "%d", &num);

但发送字符串8-9 10在两个行返回8简单,没有说明这个字符串的无效性。

But sending the string "8 -9 10" in both of the lines returned simply 8, without indicating the invalidity of this string.

任何人都可以提出一个替代?

Can anyone suggest an alternative?

推荐答案

有一个在与strtol(),它可以告诉你有关指针返回字符串的无效部分。

Have a look at strtol(), it can tell you about invalid parts of the string by pointer return.

和提防热心例如code的..看到COM prehensive错误处理手册页。

And beware of enthusiastic example code.. see the man page for comprehensive error-handling.

这篇关于确定一个C字符串在C有效的int的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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