strtol, strtoll


strtol, strtoll

Converts a string into a long (or long long) integer value

 #include <stdlib.h> long strtol ( const char * restrict s , char ** restrict endptr , int base  ); long long strtoll ( const char * restrict s , char ** restrict endptr ,                    int base  );         (C99) 

The strtol( ) function attempts to interpret the string addressed by its first pointer argument, s, as an integer numeric value, and returns the result with the type long. strtoll( ) is similar, but returns long long. The character string is interpreted as a numeral to the base specified by the third argument, base, which must be 0 or an integer between 2 and 36. If base is 36, then the letters from a to z (and likewise those from A to Z) are used as digits with values from 10 to 35. If base is between 10 and 35, then only those letters up to the digit value of base minus one are permissible. In other locales besides the default locale, C, other character sequences may also be interpretable as numerals.

If base is zero, then the numeral string is interpreted as octal if it begins, after an optional plus or minus sign, with the character 0, hexadecimal if it begins with 0x or 0X, or decimal if it begins with a digit from 1 to 9. Leading whitespace characters are ignored, and the string converted ends with the last character than can be interpreted as part of the numeral.

The second parameter, endptr, is a pointer to a pointer. If its argument value is not a null pointer, then the function stores a pointer to the first character that is not part of the numeral converted in the location addressed by the endptr argument. (The locations that the function reads from and writes to using its restricted pointer parameters must not overlap.) If no conversion is possible, the function returns 0.

If the resulting value exceeds the range of the function's type, then the return value is LONG_MAX or LONG_MIN, depending on the sign (or LLONG_MAX or LLONG_MIN, for strtoll( )), and the errno variable is set to the value of ERANGE ("range error").

Example

 char date[ ] = "10/3/2005, 13:44:18 +0100", *more = date; long day, mo, yr, hr, min, sec, tzone; day = strtol( more, &more, 10 ); mo  = strtol( more+1, &more, 10 ); yr  = strtol( more+1, &more, 10 ); /* ... */ 

See Also

strtoul( ); strtod( ), strtof( ), and strtold( ); wcstol( ) and wcstoul( ); strtoimax( ), strtoumax( ), wcstoimax( ), and wcstoumax( )



C(c) In a Nutshell
C in a Nutshell (In a Nutshell (OReilly))
ISBN: 0596006977
EAN: 2147483647
Year: 2006
Pages: 473

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net