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

Victor Kan kan at dg-rtp.dg.com
Fri Dec 22 10:27:52 AEST 1989


In article <15494 at haddock.ima.isc.com> karl at haddock.ima.isc.com (Karl Heuer) writes:
>In article <11467 at csli.Stanford.EDU> poser at csli.stanford.edu (Bill Poser) writes:
>>In article <984 at ux.acss.umn.edu> eric at ux.acss.umn.edu (Eric D. Hendrickson)
>>writes the equivalent of:
>>int is_integer(char *s) { return(regexec(regcomp("[-+]?[0-9]+"),s)); }
>
>I think regexp is a bit of overkill; I generally use strtol() for this.
>(Usually, once you've detected that the argument is in fact an integer, you
>need to evaluate it anyway, right?)

You could also use sscanf(), although some people like to avoid the
scanf() functions religiously.

Since scanf returns the number of conversions successfully made, it can
serve as an is_integer() function as well as an atoi() type thing.

int
is_integer (s, value)
char *s;
int *value;
{
	/* Add your appropriate NULL checks, etc.
	 */
	return (sscanf (s, "%d", value));
}


>And if you're on a backward system that doesn't have strtol(), it's not hard
>to write one.  Compared to regexp, for instance.

Definitely!  I took a 1.5 credit course (required) called Software Design
Laboratory a few years ago.  We, ten undergrads (mostly runny nosed 
sophomores & juniors), were given an assignment to write an EGREP 
equivalent from scratch in two weeks.  

We weren't allowed to use the system provided re_comp and re_exec 
functions, unless we could magically figure out their inner workings 
without access to source code.  Suffice it to say that even with a 
one week extension, no one in the class finished it, except one really
smart guy who already knew C/Unix before taking the course.

But I digress.

>Karl W. Z. Heuer (ima!haddock!karl or karl at haddock.isc.com), The Walking Lint

| Victor Kan               | I speak only for myself.               |  ***
| Data General Corporation | Edito cum Emacs, ergo sum.             | ****
| 62 T.W. Alexander Drive  | Columbia Lions Win, 9 October 1988 for | **** %%%%
| RTP, NC  27709           | a record of 1-44.  Way to go, Lions!   |  *** %%%



More information about the Comp.lang.c mailing list