Path: utzoo!attcan!utgpu!jarvis.csri.toronto.edu!cs.utexas.edu!swrinde!zaphod.mps.ohio-state.edu!mips!excelan!crdgw1!crdos1!davidsen From: davidsen@crdos1.crd.ge.COM (Wm E Davidsen Jr) Newsgroups: comp.lang.c Subject: Re: Is There A Way To Check If argv[1] Is An Integer? Message-ID: <1919@crdos1.crd.ge.COM> Date: 18 Dec 89 13:40:46 GMT References: <984@ux.acss.umn.edu> Organization: GE Corp R&D Center, Schenectady NY Lines: 22 Reply-exos:@crdgw1:To: davidsen@crdos1.crd.ge.com (bill davidsen) In article <984@ux.acss.umn.edu> eric@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@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