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

Tom Stockfisch tps at chem.ucsd.edu
Fri Dec 29 08:51:30 AEST 1989


In article <11523 at csli.Stanford.EDU> poser at csli.stanford.edu (Bill Poser) writes:
#	tps at chem.ucsd.edu (Tom Stockfisch) suggests the following in
#place of my regexp based solution:
#	int	/* really BOOL */
#	isInteger(string)
#		char	*string;
#	{	
#		char	*endptr;
#	
#		(void)strtol( string, &endptr, 10 );
#		return	endptr != string;
#	}
#and goes on to suggest that the regexp solution involves unnecessary
#software.
#	One reason not to use this version is because it doesn't
#work. Since strtol sets endptr to point to the character that terminated
#the scan, it returns true if a non-null prefix of the string is an integer,
#which is not the same thing as testing whether the string as a whole
#is an integer.

In the absence of a precise specification, I ignored such subtleties.

#To use strtol to do this correctly you have to check
#whether endptr is the same as the end of the string.

So, add one more test -- don't add all of regexp.

#	The other reason to use the regexp approach is that it makes
#it quite simple to implement other predicates. Want to see if the
#string is a positive integer? Just get rid of the - in the regular
#expression....

In other words, use the most elaborate tool available to solve the
simplest task, in case the task becomes more complicated later?

#	Now, if you aren't loading the regexp stuff for any other
#purpose, you can keep your code size down a bit by avoiding the use
#of regexp.

Its not the overhead that I object to, its having a program unnecessarily
dependend on a non-standard library.

#But if you're using regexp anyhow (your interactive program
#does have apropos, doesn't it?) it doesn't cost much to use it here.

I'll bet the original poster has never heard of regexp, and would have to
be given explicit instructions on how to obtain a copy.
-- 

|| Tom Stockfisch, UCSD Chemistry	tps at chem.ucsd.edu



More information about the Comp.lang.c mailing list