开发者

how to check whether a number entered from keyboard isnumeric in c?

can anyone help in know how to check whether a number entered from keyboard isnumeric in c?

I tried

开发者_如何学JAVA
isdigit


You can use strtol

Just pass a second parameter different from NULL :

If endptr is not NULL, strtol() stores the address of the first invalid character in *endptr. If there were no digits at all, strtol() stores the original value of nptr in *endptr (and returns 0). In particular, if *nptr is not '\0' but **endptr is '\0' on return, the entire string is valid.

You can also see strtoull for large unsigned integer number, or strtof, strtod, strtold to check for decimal number.

If you're parsing very large number, the best solution is to read character by character, checking the value, and store it at each step in an appropriate data structure.

But keep in mind that is if you try this solution, you would have to use an extern library to handle big numbers, or rewrite the part you need with the constraints it involves.


After quick google search:

You can use ctype.h for building function like this:

int isnumeric(char *str)
{
  while(*str)
  {
    if(!isdigit(*str))
      return 0;
    str++;
  }

  return 1;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜