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

Michael Meissner meissner at skeptic.osf.org
Sat Dec 23 03:33:18 AEST 1989


In article <636 at chem.ucsd.EDU> tps at 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');
}



More information about the Comp.lang.c mailing list