Path: utzoo!attcan!utgpu!jarvis.csri.toronto.edu!clyde.concordia.ca!uunet!cs.utexas.edu!sun-barr!decwrl!shelby!csli!poser From: poser@csli.Stanford.EDU (Bill Poser) Newsgroups: comp.lang.c Subject: Re: Is There A Way To Check If argv[1] Is An Integer? Message-ID: <11467@csli.Stanford.EDU> Date: 18 Dec 89 01:35:39 GMT References: <984@ux.acss.umn.edu> Sender: poser@csli.Stanford.EDU (Bill Poser) Reply-To: poser@csli.stanford.edu (Bill Poser) Organization: Center for the Study of Language and Information, Stanford U. Lines: 28 In article <984@ux.acss.umn.edu> eric@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