Is There A Way To Check If argv[1] Is An Integer?

Bill Poser poser at csli.Stanford.EDU
Mon Dec 18 12:35:39 AEST 1989


In article <984 at ux.acss.umn.edu> eric at ux.acss.umn.edu (Eric D. Hendrickson) writes:
>I need a way to determine if argv[1] (or any argument) is an integer.

I use the following procedures to test whether a string is an integer
or a number. They make use of Henry Spencer's regular expression package,
for which you will have to include the declarations.
is_number handles exponential notation, while is_integer does not,
but it should be easy to modify the regular expression. For greater efficiency
you may wish to make these macros and to store the compiled regular
expressions.

int
is_integer(string)
char *string;
{
   return(regexec(regcomp("[-+]?[0-9]+"),string));
}

int
is_number(string)
char *string;
{
   return(regexec(regcomp("[-+]?[0-9]*([0-9]|[0-9]\\.|[0-9]\\.[0-9]|\\.[0-9])[0-9]*([Ee][-+]?[0-9]+)?"),string));
}

(I now await flames on errors in the regular expressions.)

							Bill



More information about the Comp.lang.c mailing list