Reputation: 133
can anyone help in know how to check whether a number entered from keyboard isnumeric in c?
I tried
isdigit
Upvotes: 0
Views: 668
Reputation: 12710
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.
Upvotes: 4
Reputation: 1286
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;
}
Upvotes: 1