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

Wm E Davidsen Jr davidsen at crdos1.crd.ge.COM
Tue Dec 19 00:40:46 AEST 1989


In article <984 at ux.acss.umn.edu> eric at ux.acss.umn.edu (Eric D. Hendrickson) writes:
| I need a way to determine if argv[1] (or any argument) is an integer.  How
| can this be done?  isdigit(c) will only accept integers, and argv is char.

  No need to test, argv[1] is char *, never int. It is also not char as
you suggest.

  If you want to determine if an arg starts with a digit, you can do
isdigit(argv[1][0]). If you really want to do it right, you can use:
	int n, m, foo;
	n = sscanf(argv[1], "%d%c", &m, &foo);
	switch (n) {
	case 0: /* no leading digits */
	case 1: /* a valid integer */
	case 2: /* digits with trailing chars */
	}
  In most cases this level of checking is not needed.
-- 
bill davidsen	(davidsen at crdos1.crd.GE.COM -or- uunet!crdgw1!crdos1!davidsen)
"The world is filled with fools. They blindly follow their so-called
'reason' in the face of the church and common sense. Any fool can see
that the world is flat!" - anon



More information about the Comp.lang.c mailing list