Path: utzoo!utgpu!jarvis.csri.toronto.edu!clyde.concordia.ca!uunet!samsung!usc!snorkelwacker!paperboy!skeptic!meissner From: meissner@skeptic.osf.org (Michael Meissner) Newsgroups: comp.lang.c Subject: Re: Is There A Way To Check If argv[1] Is An Integer? Message-ID: <2232@paperboy.OSF.ORG> Date: 22 Dec 89 16:33:18 GMT References: <984@ux.acss.umn.edu> <11467@csli.Stanford.EDU> <636@chem.ucsd.EDU> Sender: news@OSF.ORG Reply-To: meissner@skeptic.osf.org (Michael Meissner) Organization: Open Software Foundation Lines: 32 In article <636@chem.ucsd.EDU> tps@chem.ucsd.edu (Tom Stockfisch) writes: (deleted text asking for a way to determine if argv[x] is a number) >How about > >int /* really BOOL */ >isInteger(string) > char *string; >{ > char *endptr; > > (void)strtol( string, &endptr, 10 ); > return endptr != string; >} You really should check to see if there is any trailing non-digits, as well as properly declaring strtol. Also, you should consider whether to require the number to be decimal only, or that it can be written in C notation (decimal, octal, or hex -- I prefer allowing all three): int /* really BOOL */ isInteger(string) char *string; { extern long strtol(); char *endptr; (void) strtol (string, &endptr, 0); return (endptr != string) && (*endptr == '\0'); }