On Thu, 7 Oct 2004, Chris Wolcott wrote:

C is not my 'native' language, so I'm asking what the most efficient way
to validate a string as containing a numeric value is.  The ato?() and
sscanf() functions just stop when they encounter a non-numeric, but do
not tell you that they did.

I need to verify that a string contains a valid numeric value.  (I.E. "
+100" not "1 + 00" nor even "1+00  ", let alone "BOB   ")  (ato?() &
sscanf() would return NULL or 1 in each case.)

It sounds like strtol() is what you are looking for. From the man page:

       The  remainder  of  the  string is converted to a long int
       value in the obvious manner, stopping at the first charac-
       ter  which  is  not  a valid digit in the given base.  (In
       bases above 10, the letter `A' in either  upper  or  lower
       case  represents 10, `B' represents 11, and so forth, with
       `Z' representing 35.)

James Rich

It's not the software that's free; it's you.
        - billyskank on Groklaw

This thread ...


Follow On AppleNews
Return to Archive home page | Return to MIDRANGE.COM home page

This mailing list archive is Copyright 1997-2019 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].